How can I do a multipart Post in Dart / AngularDart

前端 未结 3 1985
小鲜肉
小鲜肉 2021-01-07 11:58

I\'ve got a REST api which assumes a multipartfile in the a post method.

Is there any way to do this kind of posts in Dart / AngularDart because all the solutions I\

3条回答
  •  死守一世寂寞
    2021-01-07 12:24

    If you need multipart for file upload, all you have to do is send a FormData object using the HttpRequest class. Example:

    import "dart:html";
    
    ...
    
    var fileData; //file data to be uploaded
    
    var formData = new FormData();
    formData.append("field", "value"); //normal form field
    formData.appendBlob("data", fileData); //binary data
    
    HttpRequest.request("/service-url", method: "POST", sendData: formData).then((req) {
      ...
    });
    

    Furthermore, if you need to allow the user to upload a file from his hard disk, you have to use a html form with an tag. Example:

    Html file:

    dart file:

    var formData = new FormData(querySelector("#myForm"));
    HttpRequest.request("/service-url", method: "POST", sendData: formData).then((req) {
      ...
    });
    

提交回复
热议问题