Reputation: 1044
I'trying to send a Multipart form data, through Flutter but the response api can't get the image file. I created a Backend just to debug my call and I got this:
My code:
Map<String, String> fields = {
"label":'-Ll7XfpsPLd_w5kz-D0m'
};
MultipartRequest request = MultipartRequest(
'POST',
Uri.parse(url),
);
request.fields.addAll(fields);
request.files.add(
MultipartFile.fromBytes(
'image',
imagem.readAsBytesSync(),
contentType: MediaType('image', 'jpeg'),
)
);
return request.send();
The server answer:
received fields:
{ label: [ '-Ll7XfpsPLd_w5kz-D0m' ],
image:
[ '\M-o\M ... A LOT OF DATA]}
Then a made a HTML page just to test:
<form method="POST" enctype="multipart/form-data" action="http://172.20.10.2:5002/upload" >
<input type="text" name="zava" value="Zava" /> <br />
<input type="file" name="image" /> <br />
<input type="submit" /> <br />
</form>
And received de server answer:
received fields:
{ zava: [ 'Zava' ] }
received files:
{ image:
[ { fieldName: 'image',
originalFilename: 'Grifo.png',
path: '/tmp/0wvX_w8W5Mw_7flqIdK0b1xX.png',
headers: [Object],
size: 114862 } ] }
What's wrong with my code? Why de Multipart does not send the file as a file?
Upvotes: 0
Views: 293
Reputation: 1044
I discovered that if you pass the optional param filename
of the MultipartFile
the request works correctly.
Just have to do something like this:
MultipartFile.fromBytes(
'image',
imagem.readAsBytesSync(),
contentType: MediaType('image', 'jpeg'),
filename: 'dummy.jpg'
)
I had opened a bug on flutter about the request without filename (if must be repaired or the documentation changed)...
Upvotes: 1