简体   繁体   中英

XUL: Direct upload to S3 but without using a file-input element

I am developing a Firefox addon that needs to upload images from the local file system to S3 directly.

The examples I could find in AWS, SO etc. explain how this can be done for web pages using the standard file-input control for image transport (with other AWS specific request signing etc., which I've got working perfectly).

http://bencoe.tumblr.com/post/30685403088/browser-side-amazon-s3-uploads-using-cors

Uploading Image to Amazon s3 with HTML, javascript & jQuery with Ajax Request (No PHP)

http://aws.amazon.com/articles/1434

However, I couldn't find any info regarding how I can upload a file without using the file-input, I know the local absolute path to the file (the addon itself writes out the file before trying to upload it to S3).

After a few hours of trying to find the "proper" way, i thought of faking the file-input like below:

var fileInput = document.createElementNS("http://www.w3.org/1999/xhtml","input");
fileInput.setAttribute('type', 'file');
fileInput['files'] = [{
    mozFullPath: file.path,
    name: file.leafName,
    size: file.fileSize,
    type: 'image/gif' // testing only with gifs
}];

and then fd.append("file", fileInput.files[0]); where fd is my FormData object. And then POSTing it to s3 via AJAX.

This uploads the file to S3. However, when opening the file in my browser, I get the text "undefined" instead of the image.

Then i tried fd.append("file", fileInput.files); - This time the content returned by the browser instead of the image is the text "[object FileList]"

I'm stumped! I know it has got to do with trying to fake the file-input. I believe there should be a cleaner, more correct way of doping this.

Can anyone shed some light on how i can solve this? Thanks in advance.

From chrome code (aka. privileged code) you can construct DOM File objects simply by just invoking the File constructor with a path:

var file = File("path/to/some/file");
fd.append("file", file);

For more information see: Using the DOM File API in chrome code .

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