Excel .xlsx 下载 - 打开和保存组合时共享违规错误消息
我发现这个问题被提出过几次,但还没有明确的答案。看起来一个不错的解决方案可以帮助很多人。
因此,我们在服务器上创建一个 Excel 文件 (.xlsx),并使用内容配置“附件”将其下载到客户端。将出现“打开保存”对话框,如果您选择“保存”或“打开”,然后单击“另存为...”,则一切正常。但是,如果您选择“打开”,然后单击“保存...”按钮,则 Excel 会挂起一会儿,然后显示以下消息: 由于共享冲突,您的更改无法保存到“Export[5].xlsx”。尝试保存到不同的文件。 然后: 您尝试打开的文件“F8CAC020.IE5\HM2NBE5C\F8CAC020”的格式与文件扩展名指定的格式不同。打开文件之前,请验证文件是否未损坏且来源可靠。您想现在打开该文件吗? 然后: 您最终会看到“另存为:”对话框(假设您按了“是”)。
创建文件的代码是:
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AppendHeader("content-disposition", "attachment;filename=Export.xlsx")
Response.AddHeader("Content-Length", New System.IO.FileInfo(NewFile).Length)
Response.TransmitFile(NewFile)
Response.Flush()
如果删除内容处置标头,您将在 Excel 中获得正确的行为,但名称错误,并且不能保证 Excel 文档不会显示在浏览器中(取决于客户端设置) )。如果您按“打开”,然后按“保存”,那么您 收到消息: “default.aspx”是只读的。要保存副本,请单击“确定”,然后在“另存为”对话框中为工作簿指定一个新名称。
所以问题是如何将这两种行为结合成一种合理的行为: 当您尝试打开并保存下载的文件时,会直接收到上面的不错的错误消息,同时还指定应下载该文档并为其指定默认文件名。
干杯,
詹姆斯
I've found this question posed a few times, but no definitive answers as yet. Looks like a decent solution would help quite a few people.
So we create an excel file (.xlsx) on the server and download it to the client using content-disposition 'attachment'. The Open Save dialog appears and all works fine if you choose Save, or Open and then Save As... However if you choose Open and then hit the Save... button then Excel hangs for a moment and then presents the message:
Your changes could not be saved to 'Export[5].xlsx' because of a sharing violation. Try saving to a different file.
then:
The file you are trying to open 'F8CAC020.IE5\HM2NBE5C\F8CAC020', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?
then:
you finally get the Save As: dialog (assuming you pressed 'Yes').
Code for creating the file is:
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AppendHeader("content-disposition", "attachment;filename=Export.xlsx")
Response.AddHeader("Content-Length", New System.IO.FileInfo(NewFile).Length)
Response.TransmitFile(NewFile)
Response.Flush()
If you remove the content-disposition header you get the right sort of behaviour in Excel, but with the wrong name and no guarantee that the Excel document will not instead be shown in the browser (depending on client settings). If you press Open and then Save then you
get a message:
'default.aspx' is read-only. To save a copy, click OK, then give the workbook a new name in the Save As dialog box.
So the question is how to combine the 2 behaviours into a reasonable one:
Get the nice error message directly above, when you try to Open and then Save a downloaded file, but also specify that the document should be downloaded and given a default filename.
Cheers,
James
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 IE 文件下载代码中存在一个错误,该错误不会确认从 Web 服务器完成的文件传输(下载),因此在尝试保存时,与下载的(临时)文件的连接保持打开状态。这会生成“共享违规”消息。我在 FireFox 上尝试了相同的代码,但没有得到相同的结果,这肯定让我相信这是 IE 特有的问题。
不幸的是,鉴于我认为这是 IE 中的一个错误,我认为目前还没有一个干净的解决方案来解决这个问题。
I believe there is a bug in the IE file download code that does not acknowledge the completed file transfer (download) from the web server, thus leaving a connection to the downloaded (temporary) file open when the save is attempted. That generates the "sharing violation" message. I tried the same code with FireFox and I don't get the same result, which surely leads me to believe this is an IE-specific problem.
Unfortunately, given that I believe this to be a bug in IE, I don't think there is presently a clean resolution to this issue.