cordova-plugin-file-transfer
cordova-plugin-file-transfer copied to clipboard
File Transfer HTTP Method
Is there any way to specify the HTTP method of the file transfer plugin?
I have this for my file upload:
const options: FileUploadOptions = {
fileKey: 'file',
fileName: imageData.substr(imageData.lastIndexOf('/') + 1),
headers: {},
httpMethod: 'POST',
chunkedMode: false,
params: { action : 'INSERT', lineid : this.lineId }
};
But it is being received as a GET request on my server side (PHP).
Are you sure you're using the upload
api? For upload, the http method is forced to either PUT
or POST
.
See:
https://github.com/apache/cordova-plugin-file-transfer/blob/dc8fbd78a234bd88450ebe01835c60371bc2cf8e/www/FileTransfer.js#L127-L131
@breautek Certain. This is my code:
this.fileTransfer.upload(imageData, encodeURI(url.replace('https', 'http') + 'pages/attachments.php'), options).then(data => {
console.log(data);
}, error => {
console.log(error);
});
On my app, I'm using version 1.7.2-dev
, and it appears to work for me, at least for iOS and android. I have my own fork, and I am 4 commits behind Apache's master.
Note that this plugin is deprecated so there won't be any new releases or PRs merged in.
If you open up your project in Xcode (platforms/ios/) or Android Studio (platforms/android/), you should be able to set breakpoints to ensure the requests are being sent as a POST
request from the native code.
Hi @breautek
Can you please suggest me the way how to add requestBody in fileTransfer plugin. I am adding it in params but it is not working giving error as "Missing fields are required"
FileTransferError {code: 1, source: "content://com.android.providers.media.documents/document/document%3A33", target: "http://192.168.1.6:9001/protected/", http_status: 400, body: "<?xml version="1.0" encoding="UTF-8"?>↵<Error><Cod…34ec-b728-4fb5-a2fb-1beb859876db</HostId></Error>", …}body: "<?xml version="1.0" encoding="UTF-8"?>↵<Error><Code>MissingFields</Code><Message>Missing fields in request.</Message><BucketName>protected</BucketName><Resource>/protected/</Resource><RequestId>168FD184D26AB3F2</RequestId><HostId>f83934ec-b728-4fb5-a2fb-1beb859876db</HostId></Error>"code: 1exception: "http://192.168.1.6:9001/protected/"http_status: 400source: "content://com.android.providers.media.documents/document/document%3A33"target: "http://192.168.1.6:9001/protected/"__proto__: Object
let options: FileUploadOptions = {
fileKey: 'file',
fileName: res[0].filename,
httpMethod: res[0].http_method,
params: requestBodyObj,
chunkedMode: false
};
this.fileTransfer
.upload(filePath, encodeURI(res[0].upload_url), options)
.then(
data => {
console.log(data);
},
err => {
console.log(err);
}
);