transformRequest:angular.identity在angular-4中不起作用

时间:2017-09-17 10:58:02

标签: angular typescript file-upload http-headers multipartform-data

我正在尝试使用Form-data发送文件,但是这个脚本在angular-4中不起作用,尽管它在angular-2中工作

var fd = new FormData();
var datas = $("#fileForm").serializeArray();
// send other data in the form
for( var i = 0; i < datas.length; i++ ) {
    fd.append(datas[i].name, datas[i].value);
};
// append file to FormData
fd.append("file", $("#fileInput")[0].files[0])
// for sending manual values
fd.append("email", this.email+'');
fd.append("category", this.cate+'');
fd.append("pricepackage", this.pkg+'');
fd.append("duration", this.pkg_d+'');
fd.append("secret_key", this.srt_key+'');

var url = "http://127.0.0.1:8000/func/textfile_sent/";
this.http_obj.post(url,fd, {
    headers: {'Content-Type': undefined},
    transformRequest : angular.identity
}).success(function(data, status, headers, config) {
  // this callback will be called asynchronously
  // when the response is available
}).
error(function(data, status, headers, config) {
  // called asynchronously if an error occurs
  // or server returns response with an error status.
});

transformRequest : angular.identity显示错误,错误为:

  

TS2345:类型'{headers:{'Content-Type'的参数:undefined; }; transformRequest:any; }'不能分配给'RequestOptionsArgs'类型的参数。对象文字只能指定已知属性,'requestOptionsArgs'类型中不存在'transformRequest'

1 个答案:

答案 0 :(得分:0)

目前尚不清楚Angular的哪个版本发生了变化,但是在当前版本的Angular中,RequestOptionsArgs只有以下属性:

body,headers,method,params,responseType,search,url,withCredentials。

所以只需将代码更改为:

//...
this.http_obj.post(url,fd, {
    headers: {'Content-Type': undefined}, //remove the extraneous property
}).success(function(data, status, headers, config)