Phonegap fileupload / multipart to streamwork

时间:2011-04-05 16:20:43

标签: android file-upload cordova multipart

我正在尝试将带有phonegap的文件上传到流水线(https://streamwork.com/api/Post_an_item_File_item.html)。

我尝试过Phonegap的FileTransfer api,似乎没有像streamwork那样发送请求:     

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+='Content-Transfer-Encoding: '+enc+'\r\n\r\n';
body+=data+'--' + boundary + '--\r\n';
xhr.setRequestHeader('Content-length', body.length);

xhr.send(body);

}

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>

当我赋予它跨域权限的权限时,这可以在firefox中运行,但是当我在我的android(htc desire hd)上执行它时它不起作用。

Firefox xhr readystate更改输出为:1,1,2,4(0) Android xhr readystate更改输出为:1,4(0)

有没有人对我缺少的东西有所了解?

提前致谢 最大

0 个答案:

没有答案
相关问题