Raghav Patnecha
Raghav Patnecha

Reputation: 736

Not receiving the full base64 encoded data using ajax

I have a audio blob which I am converting to base64 and using ajax to pass it to a php page so that it can be stored on my sql server. But for some reason I am not receiving the full base64 file in mysql database.

Here is my code for conversion of blob to base64:

var reader = new FileReader();
reader.readAsDataURL(blob); 
reader.onloadend = function() {
base64data = reader.result;                
//console.log(base64data);
tophp(ase64data);}  // sending it to ajax function.

Now using ajax to pass it to php.

function tophp(sendtosql, base64data) {
console.log('ok');
console.log(base64data);
    $.ajax({
data: 'blob=' + encodeURIComponent(base64data),
cache: false,
processData: false, 
contentType: "application/x-www-form-urlencoded",
url: 'http://localhost/Voice/view_notification.php',
method: 'POST', // or GET
success: function(msg) {
       alert(msg);
                }
});
 }

Finally using php to insert in database:

<?php
 echo $_POST['blob'];
 $audio = $_POST['blob'];
 $sql = "INSERT INTO voice (audi) VALUES( '" . $audio . "')";
 mysqli_query($conn, $sql);
 ?>

The data is getting inserted in mysql table but when I compare the table data with my original base64 data(which I obtained from blob to base64 conversion) I don't get same results and thus affecting my audio file.

I checked the upload limit in php.ini file. It is set to 64Mb so no problem there. Also, I tried to change the data type from TEXT to BLOB in mysql table. Still no effect.

This is the sample base64 I obtained from blob conversion. Sample base64

Upvotes: 0

Views: 395

Answers (1)

Patrick Evans
Patrick Evans

Reputation: 42736

All column data types in Mysql have a size limit. In the cases of blob types they are

https://dev.mysql.com/doc/refman/5.7/en/storage-requirements.html#data-types-storage-reqs-strings

BLOB, TEXT               L + 2 bytes, where L < 2^16 
MEDIUMBLOB, MEDIUMTEXT   L + 3 bytes, where L < 2^24 
LONGBLOB, LONGTEXT       L + 4 bytes, where L < 2^32

So since your sample data is roughly 650kb it is larger than the BLOB max size of 64KB so it gets truncated.

Change the data type to one that has a larger max size, like MEDIUMBLOB or LONGBLOB. After that as long as your data does not go over those data max sizes it should not be truncated.

Upvotes: 1

Related Questions