开发者

Phonegap fileupload / multipart to streamwork

I'm triing to upload a file with phonegap to streamwork ( https://streamwork.com/api/Post_an_item_File_item.html ) .

I tried Phonegap's FileTransfer api which does not seem to send the request like streamwork wants it to:

var url =getItemsURL();

var item =  "<?xml version='1.0' encoding='UTF-8'?>" +
"<item name='Test "+timestamp()+"'>" +
"<description>my upload</description>" +
"<file_item/>" +
"</item>";
var win = function(r) {
    console.log("Code = " + r.responseCode);
    console.log("Response = " + r.response);
    console.log("Sent = " + r.bytesSent);
}

var fail = function(error) {
    alert("An error has occurred: Code = "+ error.code);
}

var options = new FileUploadOptions();
options.fileKey="file";
options.fileName="hello.txt";
options.mimeType="text/plain";

var params = new Object();
params.item = item;

options.params = params;

var paths = navigator.fileMgr.getRootPaths();
var ft = new FileTransfer();
ft.upload(paths[0] + "hello.txt",url, win, fail, options);
</code>

(actually I see that phonegap internally throws "java.io.FileNotFoundException: https://streamwork.com/v1/activities/..."

But the url should be correct since I then used the same in a manually written xhr.

function doUpload(data,fname,type,enc){

var url = getItemsURL();

var item =  "<?xml version='1.0' encoding='UTF-8'?>" +
"<item name='My new activity item "+timestamp()+"'>" +
"<description>my upload</description>" +
"<file_item/>" +
"</item>";

var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function(){      
    alert("xmlHttpObject.readyState = " + xhr.readyState + (xhr.readyState >= 3 ? " HTTP-Status = " + xhr.status : ''));
};
xhr.open("POST",url, true);
var boundary ="mime_boundary"
xhr.setRequestHeader("Accept", 'application/xml');
xhr.setRequestHeader("Content-Type", 'multipart/form-data; boundary=' + boundary);

var body = '--' + boundary + '\r\n';
body+='Content-Disposition: form-data; name="item"; filename="dummy.xml"\r\n';
body+=  'Content-Type: application/xml\r\n\r\n';
body+=item+'\r\n';
body+='--'+boundary+'\r\n\r\n';
body+='Content-Disposition: form-data; name="file"; filename="'+fname+'"\r\n';
body+='Content-Type: '+type+'\r\n';
body+='Con开发者_如何学运维tent-Transfer-Encoding: '+enc+'\r\n\r\n';
body+=data+'--' + boundary + '--\r\n';
xhr.setRequestHeader('Content-length', body.length);

xhr.send(body);

}

which I call with doUpload("lorem ipsum foo bar","test.txt","text/plain","binary");

this works in firefox when I give it the privilege to to cross domain stuff, but when I execute it on my android (htc desire hd) it does not work.

Firefox xhr readystate change outputs are: 1, 1, 2, 4(0) Android xhr readystate change outputs are: 1, 4(0)

Does anyone has an idea, on what I am missing?

Thanks in advance max

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜