I am trying to use the FormData class to send data to my server with HttpRequest.send(). I need to do a POST request with multiple fields. It should work the same as this Javascript code:
//Upload File
var uploadFile = function(file, tag, callback)
{
var xhr = new XMLHttpRequest();
xhr.open('POST', "upload/", true);
xhr.onreadystatechange=function()
{
if (xhr.readyState==4 && xhr.status==200)
{
callback();
}
}
var formData = new FormData();
formData.append('file', file);
formData.append('tag', tag);
var csrftoken = $.cookie('csrftoken');
xhr.setRequestHeader("X-CSRFToken", csrftoken);
xhr.send(formData);
};
But FormData doesn't seem to work the same way in Dart. Could someone explain how to do this in Dart, if it is possible?
Unfortunately this looks to be a bug. I've opened issue http://code.google.com/p/dart/issues/detail?id=7152 to track. Thanks for the use case.
You have to use FormData.appendBlob :
void uploadFile(File file, String tag, callback) {
final xhr = new HttpRequest();
xhr.open('POST', "upload/", true);
xhr.on.readyStateChange.add((e) {
if (xhr.readyState == 4 && xhr.status == 200) {
callback();
}
});
final formData = new FormData();
formData.appendBlob('file', file);
formData.append('tag', tag);
xhr.send(formData);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With