I need to upload files along with other data from a Nativescript app. Here is what I am doing:
const data = new FormData();
data.append('name', 'Some Name');
data.append('profile_picture', profilePic);
Here profile pic is a nativescript File object. It does not seem to work as expected. It is sent to server as "[object object]".
How to do upload the file in form data?
I need to upload files along with other data from a Nativescript app. Here is what I am doing:
const data = new FormData();
data.append('name', 'Some Name');
data.append('profile_picture', profilePic);
Here profile pic is a nativescript File object. It does not seem to work as expected. It is sent to server as "[object object]".
How to do upload the file in form data?
Share Improve this question edited Sep 5, 2016 at 16:50 RPichioli 3,3453 gold badges27 silver badges31 bronze badges asked Sep 5, 2016 at 16:34 Prabhakar BhatPrabhakar Bhat 1,4632 gold badges12 silver badges16 bronze badges 4- Have your tried this? nativescript.github.io/nativescript-background-http – inf3rno Commented Sep 5, 2016 at 18:07
- 4 Yes, it only allows to upload files directly as octet stream. Not as part of formdata. – Prabhakar Bhat Commented Sep 5, 2016 at 18:31
- 2 If you want to send it as a form data, you could encode your picture in base64 – Kansen Commented Sep 6, 2016 at 15:05
- 1 That's fine for picture, but elsewhere I need to send video as well. base64 may not be the best way. – Prabhakar Bhat Commented Sep 8, 2016 at 6:18
2 Answers
Reset to default 4Since January https://github./NativeScript/nativescript-background-http supports multipart uploads, too.
Example:
var request = {
url: url,
method: "POST",
headers: {
"Content-Type": "application/octet-stream",
"File-Name": name
},
description: description
};
var params = [{name: "test", value: "value"}, {name:"fileToUpload", filename: file, mimeType: 'image/jpeg'}];
task = session.multipartUpload(params, request);
Binary support should be ing in NativeScript v6.3
. See this issue. You should then no longer need to use nativescript-background-http
. You can see my full write-up here.