简体   繁体   中英

upload / POST file with html5 / ajax / javascript without using form and <input type=file>

I need to upload a file to a server in post without using a form or even a tag.

The reason is because i'm working on a web app for a mobile device and the file sections is done via the integrated gallery app. It returns me only the path to the picture on the device file system.

I assume this may be done by reading the filecontent, putting it in a blob and using a FormData object ? Something like :

var oMyForm = new FormData();

oMyForm.append("id", "foo");
oMyForm.append("user", "bar");

var oFileBody = someFileReadingFunction("/path/to/picture"); // I think this should be some custom javascript call specific from the device.
var oBlob = new Blob([oFileBody], { type: "image/jpg"});

oMyForm.append("mypicture", oBlob);

var oReq = new XMLHttpRequest();
oReq.open("POST", "http://foo.com/submitform.php");
oReq.send(oMyForm);

I'm right or i'm I looking in the wrong direction ?

Best regards

For the part of reading picture from device storage you will probably have to do it in Tizen Native code. You can read about the communication between Tizen Web and Tizen Native here:

Calling Native from Web

Calling Web from native

There are an examples which should help you to understand how to handle this communication in both ways.

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