danialfarid / ng-file-upload

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support
MIT License
7.87k stars 1.6k forks source link

Using New File(...) in Safari results in empty files being uploaded #2049

Open oodavid opened 6 years ago

oodavid commented 6 years ago

As the title states.

The following code demonstrates the issue on Safari (tested on 10.1.2)

You'll need to plug in some S3 details for it to work.

// S3 details
var S3Url = 'https://angular-file-upload.s3.amazonaws.com/';
var S3Policy = { ... };
var S3Signature = { ... };
var AWSAccessKeyId = '...';

// Create a new file
var file = new File(["foo"], "foo.txt", {
  type: "text/plain",
});
var fileKey = Date.now() + file.name;
var filePath = S3Url + fileKey;

// Upload to S3
Upload.upload({
    url: S3Url,
    method: 'POST',
    data: {
        key: fileKey,
        AWSAccessKeyId: AWSAccessKeyId,
        acl: 'public-read',
        policy: S3Policy,
        signature: S3Signature,
        "Content-Type": file.type,
        filename: file.name,
        file: file
    }
})
.success(function(xmlResponse, status, headers, config){
    if(status === 200 || status === 201){
        console.log('success', filePath);
    } else {
        console.log('err', status);
    }
})
.error(function(err) {
    console.log('err', err);
});

You will see a successful upload, however, if you view the file, it will be empty. Expected to contain the text "foo". The same code works OK in latest Chrome and Firefox.