Reputation: 1114
So what I am trying to do is to replicate the process of uploading images to my server (POST method) in PhoneGap using fileTransfer.
For testing purposes I used Chrome's Postman Rest client and this is the configuration that works:
and here is the JS code for Phonegap:
// imageData is the url to image (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
And the response:
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
Any help would be VERY very much appreciated as I completely stuck:(
Upvotes: 0
Views: 1750
Reputation: 111
Please follow the code mentioned below :
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);
Also let me know whether you are working for ANDROID or iOS? Because you may face some issues in the Android code.
Upvotes: 2