我有一个Web API,可以将文件保存为字节流。响应已经被获取并保存在状态中,但是现在我想在单击按钮时从我的React应用程序中下载文件。我按如下方式操作:
downloadContract( binaryData ) {
const file = new Blob([binaryData], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
调试后,流正确获取,但下载文件时出现错误:加载PDF文件时出错。
更新:
使用此来源进行新的端点调用:
callLoadContract: {
remote( state, id, contractId ) {
const url = `${base}/vendor/${id}/${contractId }`;
return $http.instance.api.get( url, id, contractId);
},
success: Actions.contractLoaded,
error: Actions.fail
}
处理响应:
loadContract({id, contractId}) {
this.getInstance().callLoadContract( id, contractId );
}
contractLoaded( response ) {
if (response && response.data) {
console.log(response);
const file = new Blob([response.data], { type: 'application/pdf' });
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
}
}
同样的错误。
/api/download?fileId=XXXX
? - drinchev