使用Phonegap上传文件:文件传输错误:请求正文流耗尽

时间:2014-03-26 03:44:50

标签: javascript cordova file-transfer

所以我要做的是使用fileTransfer复制在PhoneGap中将图像上传到我的服务器(POST方法)的过程。

出于测试目的,我使用了Chrome的Postman Rest客户端,这是可行的配置:

enter image description here

这是Phonegap的JS代码:

// imageData是图像的网址(file:/// var / mobile / ...)

var options = new FileUploadOptions();
options.fileKey = "media";
options.fileName=imageData.substr(imageData.lastIndexOf('/')+1);
options.mimeType="image/jpeg";

var params = new Object();
params.category = "/api/v1/category/testcat/";
params.message = "Hello from iOS";
options.params = params;

var ft = new FileTransfer();
ft.upload(imageData, encodeURI("http://mydomain.com/api/v1/post/"), win, fail, options);

// win, fail

回复:

2014-03-25 23:30:46.582 StarterApp[6103:6107] FileTransferError {
    body = "";
    code = 3;
    "http_status" = 0;
    source = "file:///var/mobile/Applications/3FEF7630-C089-4DF3-9618-EF9EEE0AD6FE/tmp/cdv_photo_002.jpg";
    target = "http://mydomain.com/api/v1/post/";
}
2014-03-25 23:30:46.584 StarterApp[6103:6107] File Transfer Error: request body stream exhausted

任何帮助都会非常感激,因为我完全陷入困境:(

1 个答案:

答案 0 :(得分:2)

请遵循以下提及的代码:

var options = new FileUploadOptions();
options.headers = {
Connection: "close"
}
var options = new FileUploadOptions();
options.chunkedMode = false;
options.fileKey="image";
options.fileName=global_URI.substr(global_URI.lastIndexOf('/')+1)+'.jpg';
options.mimeType="image/jpeg";
var params = new Object();
options.params = params;
params.user_id =userID;
params.group_id=groupId;
var url=encodeURI(ipAddress +"/add_content.php");
var ft = new FileTransfer();
ft.upload(global_URI,url,win, fail, options);

另请告诉我您是在为ANDROID还是iOS工作?因为您可能在Android代码中遇到一些问题。