我正在尝试将带有 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);
}
which I call with
doUpload("lorem ipsum foo bar","test.txt","text/plain","binary");
当我授予它跨域内容的特权时,这在 Firefox 中有效,但是当我在我的 android(htc 希望 hd)上执行它时,它不起作用。
Firefox xhr 就绪状态更改输出为:1、1、2、4(0) Android xhr 就绪状态更改输出为:1、4(0)
有没有人知道我错过了什么?
预先感谢最大