4

In my asp page, I have to open a csv file in IE by java script. The code which I am using is as below:

csvWindow = window.open("/com/csv/"+csvFileName, "datacsv", "toolbar=yes,location=no,directories=yes,status=no,menubar=yes,scrollbars=yes,resizable=yes,width=790,height=450,screenX=50,screenY=25,left=50,top=25");

Code is running in IIS server.

When I run this code and open csv file it gives below message

 Microsoft Office Excel cannot access the file
      "http://192.168.3.228:107/com/csv/CSV_file_1345728.csv". 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 yo save has the same name as a currently open workbook.

But file is being created.So path is correct and i think that file is also not used by another program

Please help me what should I do

8
  • Make sure the file exists on the specified location at the moment you are making a call to it (like the error message says). Then,, everything should be fine. Commented Jan 4, 2012 at 8:05
  • @Baszz, yes file exist at the place but at IE when I paste the same url "192.168.3.228:107/com/csv/CSV_file_1345728.csv" then also this message is shown, so could u please tell me what is wrong or is there any other method to open the file Commented Jan 4, 2012 at 8:09
  • Have you tried to rename your csv file? I mean, give it a simple name & check if it contains any special character. Commented Jan 4, 2012 at 8:37
  • how are you creating the file? if programmatically, are you closing all connections to it once complete (such as a Scripting.TextStream)? Commented Jan 4, 2012 at 8:52
  • @oracle certified professional, yes I am proper closing the file Commented Jan 4, 2012 at 8:58

2 Answers 2

5
+25

The problem is that when Excel is opened it will attempt to fetch the CSV file itself, this a change in behaviour in office apps since 2007. However since Excel runs in a different process it will not send any cookies that would have been generated during the logon. When the website receives the request it will respond with a 401 status.

There are no easy solutions that I know of with entirely satisfactory results. Here are a number of solutions but all have drawbacks.

  • Make the authentication cookie persistent, this will allow Offices apps to pick up and send the cookie. The down side being the user remains persistently logged even after a client machine reboot (much like how Stackoverflow appears to work).
  • Use a standard HTTP authentication protocol like "Basic" or "Negotiate". The down side is that this will cause Excel to display a logon box and the user has to logon again. One exception to this drawback is using "Negotiate" or "NTLM" against an IIS box where the site is registered as part of the IE's Intranet Zone, in which case the HTTP stack used by excel will attempt to use the current user credentials.
  • Have a server side script that can run anonymously send the csv file and include in the URL some unique ID (such as GUID) which is a one off grant of access. Much more complex to set up.
Sign up to request clarification or add additional context in comments.

2 Comments

the code is workin on Mozila firefox , but not at IE.I am using IE-7
@user571616: Yes because Firefox uses its own HTTP stack that doesn't pay attention to the WinINET mime handlers. Hence it just downloads the csv then asks "Right who wants to handle this CSV content I've got?". On the other hand IE uses WinINET and has much tighter integration in both windows and office, it determines quite early that the resource being requested is of interest to excel and Excel (or actually Office) takes over handling the request.
3

If you want to open the file with MS Excel, you could try not to serve the file directly, but write an ASP page with Content-Type=application/force-download, the real file name ending with .css and the actual file content. In this case, MSIE will first download the file to the local disk cache and then will feed it to MS Excel.

If you just want to show the CSV text in the browser window, maybe the best is to change its extension or to make some proxy page with Content-Type=text/plain and no mention of CSV at all. The association CSV/Excel seems to be hardcoded in MSIE.

2 Comments

is there a way to remove this association of csv/excel from MSIE
@user571616 I don't think so. I have some experience with serving file responses and I know that, for example, *.xls will always be opened by MSIE with MS Excel regardless of Content-Type and the actual content. Since 2000, this hack permits opening generated Office HTML pages as if it were binary Office documents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.