我和团队成员一直在努力使多个文件上传组件与asp.net后端配合工作。团队成员编写了后端,因此我对api的确切功能知之甚少,但是当我们使用Postman发送请求时,该文件确实到达并且在模型调试器中可见。
我尝试了各种方法从我的React前端发送文件,包括作为状态的直接副本发送,作为带有附加信息的数组发送以及作为一组独立文件发送,如{ {3}}。
是的,表单正在使用
<form encType="multipart/form-data"/>
,但是文件上传本身在我设计的DropZone组件后被混淆了。
我和我的团队成员都对React还是很陌生,对此细节感到沮丧。
API调用:
const data = new FormData(form);
for(var i=0; i <this.state.files.length; i++){
filesArray.push('files'+ i, this.state.files.item(i));
}
data.append("files", filesArray);
fetch('http://localhost:5824/api/Claim', {
method: 'POST',
body: data,
});
DropZone的相关部分,格式为:
<input ref={this.fileInputRef} className="dropInput" type="file" multiple onChange={this.onFilesAdded}/>
<span>Upload Files</span>
答案 0 :(得分:0)
我需要做的就是将name =“ files”添加到DropZone组件中。
<input ref={this.fileInputRef} className="dropInput" type="file" name="files" multiple onChange={this.onFilesAdded}/>
请勿将文件从组件状态附加到FormData。