简体   繁体   中英

Unable to send base64 video through Ajax Post to PHP

When I try to upload a MP4 video with 16.9 MB size, using ajax async post to an PHP file the console triggers an error saying: POST http://website.com/proc_vids.php net::ERR_EMPTY_RESPONSE

I know for a fact that this problem is related with PHP memory_limit because when I set to 200 MB it's all fine but when I change it back to 100 MB this error happens.

I can't even get the POST to an PHP variable because as soon as the ajax post call is made it triggers the error without even doing anything on server side (PHP). Here is the ajax post code:

var proc = 1;
video = document.getElementById('preview_video').src;
    $.ajax({
        'async': true,
        'type': "POST",
        'global': false,
        'dataType': 'json',
        'url': "proc_vids.php",
        'data': {proc: proc, video: video}
    }).done(function () {
            //Do something
            });

PHP code:

$proc = $_POST['proc'];

if ($proc == 1){

//$video = $_POST['video'];
}

As you can see I commented the line where I pass the POST to a variable and still triggering the error.

What can I do to the video variable containing the base64 code to not expand consuming such high memory levels? Is there any alternatives without setting the memory_limit higher?

Problem solved thanks to cmorrissey!

I used the same method as described in this thread: Convert HTML5 Canvas into File to be uploaded?

Sending AJAX POST as a FormData and converting the base64 data to Uint8Array into a blob is the key to not allocate PHP memory when the POST is made. But be careful tho because older browsers may not support blob.

Thank you guys ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM