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

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

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.

Related

how to skip content block in wordpad

I writing a code in vb.net. The codes will create a .rtf (wordpad file format). in that code i'm not only write text but a picture as well. the problem is windows always ask content security everytime i add a picture (fyi, picture already exist in my computer) into that file.
this is the preview
i tried to find setting in windows and wordpad, but still got nothing
how can i skip this security warning?
thank you
Just to clarify, I'm seeing the same issue. RTF created by hand by myself, new text with a few URLs, and pasted images captured by Windows screenshot grabbing app.
Dir /r was showing no alternate stream, but I still got the annoying message box every time I open the file.
Turns out the blocked content were the images, they also won't get copied to the clipboard. Tried grabbing them again, and pasting them to Paint and back to the document, and now WordPad likes them.

How to make Firefox open RSS directly (like other XML files), instead of downloading?

Using Firefox 70.0.1 on Arch Linux. (I realize that this question might technically belong on SuperUser, but I suspect that no non-developer would ever need to do this.)
I'm trying to debug my code that generates an RSS feed, by viewing it directly in Firefox's XML viewer.
When I enter a feed URL, like https://en.blog.wordpress.com/feed/, Firefox pops up the dialog that asks me what to do with the file. It calls it an "RSS Summary", and I can choose to open it or save it to disk.
I can choose to open it in Firefox, but then the file is first downloaded to disk, and then opened from a file:// URL. The result is that I can't hit F5 to refresh it; I have to enter the URL anew.
This only happens if the Content-Type header is application/rss+xml. A file served up as plain application/xml (like e.g. https://xkcd.com/rss.xml uses) is opened directly.
How can I tell Firefox that application/rss+xml should receive the same treatment and just be viewed as a regular XML file?
Add the „view-source:“ prefix in front of your url as said in this article: https://css-tricks.com/snippets/html/view-source-of-rss-feed-in-firefox/
You should be able to achieve this using an extension.
There is Open in Browser which seems to resolve the issue.
When you try opening the RSS URL with the extension installed, you will be shown a window where you can choose which action to take. You can choose to open the file as XML in the browser window. If you want to be able to hit F5 and reload the file, you may also want to tick "Do this automatically for files like this from now on".
Note that I haven't read the source code of the extension so I don't know if it is safe to use or not.

Internet Explorer file download Excel file filename

I have a bespoke application that downloads an Excel file from the network
The Excel file has a macro within the Workbook Open event. I have noticed that if I put the following code in the event
MsgBox Application.ThisWorkbook.Name
I can see that when the file "PWABCD.XLS" is opened from Internet Explorer 8, the messagebox shows "PWABCD(1).XLS". When the same operation is done from Internet Explorer 10, the message box shows "PWABCD.XLS".
Where is the setting that says the file download name should have (1) inserted? Or is there some other conflict that is causing the (1) to be inserted into the filename? I have inherited logic that assumes the (1) will be there and removes it - rather than amend the code to check for the existence of the (1), is there a setting the user can amend?
Many thanks
The (1) is automatically added to the filename if another file with the same name exists in the destination directory. There is no way to avoid this other than testing to see if the file already exists and deleting it if it does.

How to start "save as / open" after requesting a file from server?

I'm using MooTools as JS-Framework.
When a user clicks the "Show Report"-Button on my website I'm starting Request.HTML to retrieve the path of the requested report.
If the report does not exists yet, its been created on the server (a waiting popup is shown to the user). The report file is saved to a special path on the server and this path is returned to the browser.
After I got the path to the Report-File - the Report can reach about 5 to 8MB - I want to show the user a "Download-ProgressBar" while starting a new Request.HTML to retrieve the big report file.
Everthing until this point works fine. I got the ProgressBar filling up, after its finished I got my big file.
But my problem is now, how I can start something like "Open File" or "Save File As" from JavaScript.
I got the freaking file downloaded and shown in the console but now I want the user to save this file somewhere or to open it directly...
Kind Regard.
Why don't just show a link to that file so that it opens in a new page?
That way one can open in a new window and save from the browser's menu (or use the right click>save to menu).
Since you already have your file serialized most browsers nowadays supports the data uri scheme so if this fine to support a limited subset of browsers you can do
window.open(fileToSave, '_blank');
notably old version of IE don't support all kind of files using data-uri so it will not work for html.
There are some File API in newer browser too, and there are some other solutions involving flash but historically the right way to do this is to navigate to a server page which returns an header
Content-Disposition: attachment; filename=yourfilename.html
that will work in any browser (I used this a lot in IE6 with no particular problem).

Exception with Excel SaveCopyAs() method

I have generated a excel report using excel-addins in vs2010 and it saved to desktop. After that I reopened the same file and changed it's data. When I tried to save back with same file name Exception occurs.
Code:-
Globals.ThisAddIn.Application.ActiveWorkbook.SaveCopyAs(saveFileDialog1.FileName);*
Exception:-
System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Excel cannot access the file 'C:\Documents and Settings\112\Desktop\111.xlsx'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
Check to see that your file isn't being used by something else.
You are almost certainly hitting a file access exception, probably because the document you are attempting to write is already open in another program.
I'm guessing that program is Microsoft Excel.
Close the document in Excel and re-run your test.

Resources