0

I have the following jQuery, which I think should pass a sliced file to PHP:

var blob = f.slice(start, stop);

var formData = new FormData();    
formData.append('fileUpload', blob, f.name);

$.ajax({
     url: 'save.php',
     type: 'POST',
     data: formData,
     processData: false,
     contentType: false,
     success: function(msg){
          alert("Success: " + msg);
     },
     error: function(bla, msg){
          alert("Fail: " + msg);
     }
});

However, I'm not sure what the PHP should look like to save the blob to a file.

The intention is for the browser to slice the file and then save the output.

Lee
  • 1,485
  • 2
  • 24
  • 44

2 Answers2

0

Receive it as a file at your server

<?php
  //var_dump($_FILES) // check if you received it or not
  $blob = $_FILES('fileUpload');
  file_put_contents('/path/to/new/file_name', $blob);

?>

You might want to save the file to a tmp location first, then do some checks on it before you move it to a final location (with PHPs rename() function).

Btw: why not just save the BLOB to DB? That is a legitimate way of handling files these days, that's what the BLOB MySQL data type is for after all.

I already asked this and it worked, have a look: send formData appended with blob to server

Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
-2

Due to contentType: false the data is available in $_FILES rather than $_POST. One way to do it, is like so:

<?php

    move_uploaded_file(
        $_FILES['fileUpload']['tmp_name'], 
        $_FILES['fileUpload']['name']
    ); 

?>
Lee
  • 1,485
  • 2
  • 24
  • 44