问题描述
因此,在后端,我将其保存在本地并将其作为字节流发送:
C#代码:
public async Task<IActionResult> ExportToCsv(string siteId)
{
var employeeTypeEntity = await employeeBusiness.GetEmployeeTypeAsync(siteId);
var fileName = $"{GetUserName}_employeetype-{DateTime.Now.ToString("yyyyMMddhhmmss")}.csv";
var filePath = $"{hostingEnvironment.ContentRootPath}\\{ControllerConstants.exportFileFolder}\\{fileName}";
employeeBusiness.ExportToCsv(filePath,employeeTypeEntity);
var bytes = System.IO.File.ReadAllBytes(filePath);
var result = File(bytes,"application/octet-stream",fileName);
return result;
}
结果返回:
ContentType =“应用程序/八位字节流”
EnableRangeProcessing =假
EntityTag = null
FileContents = {byte [38]}
FileDownloadName =“ _employeetype-20200925052809.csv”
LastModified = null
当用户单击导出按钮(反应代码)时,我想让它下载它。我遇到的问题并没有完全理解它是如何处理响应?通常,我将返回response.json(),并且会收到此错误“ SyntaxError:JSON在位置0处出现意外令牌S”。这使我找到了生成的代码。
javascript代码:
static exportExcelData(data) {
const accesstoken = SessionStorage.getAccesstoken()
debugger
return fetch(`some url path/${data}`,{
method: 'POST',headers: {
'Content-Type': 'application/json',Authorization: `Bearer ${accesstoken}`,},})
.then(respone => {
var res = respone.body(); // main issue
debugger
var byteArray = new Uint8Array(res)
var a = window.document.createElement('a')
var x = window.URL.createObjectURL(new Blob([byteArray],{ type: 'application/octet-stream' }))
window.open(x) // just want to see if it open with data
})
.catch(handleError)
}
无法正确处理我一直在阅读的内容。
我应该如何应对?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)