【发布时间】:2018-01-14 16:26:48
【问题描述】:
我把这个问题参考POST a file with React.js
我现在想将文件列表发送到端点。
我的输入组件:
<input onChange={(e) => Actions.uploadXLS(e.target.files)} multiple type="file" name="xlsz" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" style={{ display: 'none' }}/>
动作处理程序:
uploadXLS(files) {
let i = 0;
const data = new FormData();
for ( i = 0; i < files.length; i++ ) {
data.append( 'file' , files[i]);
}
console.log(data);
this.getInstance().callUploadXLS( data );
}
控制台打印:FormData{}
【问题讨论】:
-
FormDatawill not allow you to inspect it's object。检查文件是否正确传递的唯一方法是检查 AJAX 调用(通过网络选项卡)。console.log( data );将始终显示相同的对象,而不是文件本身。 -
你的代码应该可以工作
-
@dinchev 这是有效载荷: ------WebKitFormBoundaryx290rYq0fZYR1L6x Content-Disposition: form-data;名称=“文件”; filename="ExcelFileCore-1.xlsx" Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ------WebKitFormBoundaryx290rYq0fZYR1L6x Content-Disposition: form-data;名称=“文件”; filename="ExcelFileCore-1 - Copy.xlsx" Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ------WebKitFormBoundaryx290rYq0fZYR1L6x--
-
是的,所以你上传了两个文件
ExcelFileCore-1.xlsx,ExcelFileCore-1 - Copy.xlsx。如果工作正常,后端应该已经捕获了它们。 -
@dinchev 似乎后端工作正常。检查下面的答案。请注意,我没有从端点得到任何响应。
标签: javascript reactjs multipartform-data reactjs-flux