我需要从请求中发送的某些参数生成Excel文件。一切似乎都运行良好,但如果我打开Chrome的开发工具,它会将下载标记为红色,状态列显示“(已取消)”。我仍然可以完全打开下载的文件。
我还使用net-internals记录更多信息,但我不确定我在寻找什么。
最终的日志消息显示:
[st=52] DOWNLOAD_ITEM_COMPLETING
--> bytes_so_far = "6656"
--> final_hash = "BLAHBLAHBLAH....."
[st=80] DOWNLOAD_FILE_DETACHED
[st=80] DOWNLOAD_FILE_CLOSED
[st=93] DOWNLOAD_ITEM_FINISHED
--> auto_opened = "no"
[st=94] -DOWNLOAD_ITEM_ACTIVE
用于生成文件的Grails / apache poi代码非常简单:
def downloadFile() {
def cellData = params.data
response.setContentType("application/vnd.ms-excel")
response.setHeader('Content-Disposition', 'attachment;filename=myfile.xls')
response.setStatus(HttpServletResponse.SC_OK)
Workbook wb = new HSSFWorkbook()
Sheet sheet = wb.createSheet("My Sheet")
Row row = sheet.createRow(0)
row.createCell(0).setCellValue(cellData)
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte [] outArray = outByteStream.toByteArray();
response.setContentLength(outArray.length);
response.outputStream << outArray
}
最后,在我们的Javascript应用程序中(我们使用的是ExtJS 4)我尝试了多种下载文件的方法:
所有三个都成功下载了该文件,但Chrome仍然显示状态已取消。
我在Chrome控制台中发现了这个警告:
Resource interpreted as Document but transferred with MIME type application/vnd.ms-excel
谢谢!
答案 0 :(得分:0)
添加此行
render(text: outArray, contentType: "text/xls", encoding: "UTF-8")
之后
response.outputStream << outArray
答案 1 :(得分:0)
Adman抱歉忘了刷新,下面的代码正在处理我的chrome
def xls() {
def cellData = params.data
response.setContentType("application/vnd.ms-excel")
response.setHeader('Content-Disposition', 'attachment;filename=myfile.xls')
response.setStatus(HttpServletResponse.SC_OK)
Workbook wb = new HSSFWorkbook()
Sheet sheet = wb.createSheet("My Sheet")
Row row = sheet.createRow(0)
row.createCell(0).setCellValue(cellData)
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
wb.write(outByteStream);
byte [] outArray = outByteStream.toByteArray();
response.setContentLength(outArray.length);
response.outputStream << outArray
response.outputStream.flush
render(text: outArray, contentType: "application/vnd.ms-excel", encoding: "UTF-8")
}