Multipart request with AngularJS

后端 未结 5 1366
说谎
说谎 2020-12-30 02:20

I have an API endpoint to which I must send a multipart HTTP request, composed of two parts, file (a file system file) and data (a JSON object).

5条回答
  •  有刺的猬
    2020-12-30 02:30

    Have you tried something like this:

    $httpProvider.defaults.transformRequest = function(data) {
      if (data === undefined){
        return data;
      }
    
      var formData = new FormData();
      for (var key in data){
        if(typeof data[key] == 'object' && !(data[key] instanceof File)){
          formData.append(key, JSON.stringify(data[key]));
        }else{
          formData.append(key, data[key]);
        }
      }
      return formData;
    };
    

提交回复
热议问题