DART post with multipart/form-data

寵の児 提交于 2019-11-28 05:32:01

问题


in DART lang, how to specify POST request Content-Type to be

multipart/form-data

My DART code is:

sendDatas(dynamic data) {
final req = new HttpRequest();
req.onReadyStateChange.listen((Event e) {
  if (req.readyState == HttpRequest.DONE &&
      (req.status == 200 || req.status == 0)) {
    window.alert("upload complete");
  }
});
req.open("POST", "/upload");
req.send(data);

}

I am doing POST with a file


回答1:


I think you should use HttpRequest.postFormData(url, data) here. Then you can use the following:

FormData data = new FormData(); // from dart:html

data.append(key, value);

HttpRequest.request('/upload', method: 'POST', sendData: data).then((HttpRequest r) {
  // ...
});

Regards, Robert




回答2:


On the server it is supported by the http package. This package can also be used in the browser but it seems the multipart_request.dart file can't be imported in the browser because it uses dart:io;

I suggest creating a feature request on http://dartbug.com/new.

You could also try to copy the code from the [http] package and remove the references to the io package and use browser API instead.

How it could be used on the server is shown here http://www.dartdocs.org/documentation/http/0.11.1+1/index.html#http/http.MultipartRequest



来源:https://stackoverflow.com/questions/25742113/dart-post-with-multipart-form-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!