我正在尝试使用 ionic 2 文件传输以及其他参数将文件发送到 api。它是表单数据。这是我将数据发送到 api 端的函数:
uploadFileRemark(businessId,theFile,content,val){
const fileTransfer: FileTransferObject = this.transfer.create();
console.log(theFile);
var uploadOptions = {
fileKey: "img_url",
fileName: "img.png",
params : {'bid':businessId,"imgurl":theFile,"content":content},
headers: {'Authorization':'Bearer ' + val, 'Content-Type': 'application/x-www-form-urlencoded'}
};
//console.log(this.api.url+'/api/business/postRemark');
this.createLoadingSpinner();
this.presentLoadingSpinner();
let ft = fileTransfer.upload(theFile,this.api.url+'/api/business/postRemark', uploadOptions)
.then((data) => {
this.dismissLoadingSpinner();
console.log(data);
alert("BID"+businessId+" "+JSON.stringify(data));
}, (err) => {
this.dismissLoadingSpinner();
console.log('OOPS!!! UNSUCCESSFUL');
alert("BID"+businessId+" "+JSON.stringify(err));
})
return ft;
}
但是,如果我通过没有"bid"参数的邮递员发送请求,它会给我相同的响应。我已经检查了变量格式和其他格式。但是,它仍然不起作用。出了什么问题。
您需要更改文件传输上传选项:
var uploadOptions = {
fileKey: "file", // change fileKey
chunkedMode: false, // add chunkedMode
mimeType: "multipart/form-data", // add mimeType
fileName: "img.png",
params : {'bid':businessId,"imgurl":theFile,"content":content},
headers: {'Authorization':'Bearer ' + val, 'Content-Type': 'application/x-www-form-urlencoded'}
};