Excel xlsx download - sharing violation error message on Open and Save combination

0 votes

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

Apr 5, 2022 in Database by Edureka
• 13,690 points
1,137 views

1 answer to this question.

0 votes
I believe there is a fault in the Internet Explorer file download code that does not acknowledge the completion of the file transfer (download) from the web server, leaving an open connection to the downloaded (temporary) file when the save is attempted. The message "sharing violation" appears as a result of this. I tested the identical code in FireFox and didn't get the same result, so I'm very confident this is an IE-specific issue.

Unfortunately, because I believe this is a fault in Internet Explorer, I don't believe there is a simple cure to this problem right now.
answered Apr 5, 2022 by gaurav
• 23,260 points

Related Questions In Database

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to calculate age (in years) based on Date of Birth and getDate()

Following can be a solution to your ...READ MORE

answered Feb 23, 2022 in Database by Vaani
• 7,070 points
1,611 views
0 votes
0 answers

How to get address, Column Name and Row Name of all marked rows in Excel table as rows in new worksheet

 need the row/column combinations marked with an ...READ MORE

Feb 24, 2022 in Database by Edureka
• 13,690 points
2,067 views
0 votes
1 answer

Excel formula to remove comma, spaces, period and add a text

The steps to accomplish this are as ...READ MORE

answered Mar 15, 2022 in Database by gaurav
• 23,260 points
2,360 views
0 votes
1 answer

Hours and minutes difference between two times in Excel

Using the TEXT function in Excel is ...READ MORE

answered Mar 25, 2022 in Database by gaurav
• 23,260 points
1,181 views
0 votes
1 answer

Excel COUNTIF with multiple criteria and both row and column

Count Cells Based On Text Value Using ...READ MORE

answered Mar 25, 2022 in Database by gaurav
• 23,260 points
2,329 views
0 votes
1 answer

Pandas cannot open an Excel (.xlsx) file

XLRDError: Excel xlsx file not supported by ...READ MORE

answered Apr 4, 2022 in Database by gaurav
• 23,260 points
5,680 views
0 votes
1 answer

Excel feet and inches to millimeters

There are certain direct formulas to convert ...READ MORE

answered Feb 23, 2022 in Database by gaurav
• 23,260 points
1,856 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP