[英]Not receiving the full base64 encoded data using ajax
我有一個音頻Blob,我將其轉換為base64,並使用ajax將其傳遞到php頁面,以便可以將其存儲在我的sql服務器上。 但是由於某種原因,我沒有在mysql數據庫中收到完整的base64文件。
這是我將blob轉換為base64的代碼:
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
base64data = reader.result;
//console.log(base64data);
tophp(ase64data);} // sending it to ajax function.
現在使用ajax將其傳遞給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);
}
});
}
最后使用php插入數據庫:
<?php
echo $_POST['blob'];
$audio = $_POST['blob'];
$sql = "INSERT INTO voice (audi) VALUES( '" . $audio . "')";
mysqli_query($conn, $sql);
?>
數據正在插入mysql表中,但是當我將表數據與原始base64數據(從blob到base64轉換獲得的數據)進行比較時,不會得到相同的結果,因此會影響我的音頻文件。
我檢查了php.ini
文件中的上傳限制。 它設置為64Mb,所以在那里沒有問題。 另外,我試圖在mysql表中將數據類型從TEXT更改為BLOB。 仍然沒有效果。
這是我從Blob轉換中獲得的示例base64。 樣本base64
Mysql中的所有列數據類型都有大小限制。 對於Blob類型,它們是
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
因此,由於您的示例數據大約為650kb,它大於BLOB最大大小64KB,因此它會被截斷。
將數據類型更改為最大大小較大的數據類型,例如MEDIUMBLOB或LONGBLOB。 之后,只要您的數據不超過這些數據最大大小,就不應將其截斷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.