Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to open csv file in IE in by javascript

Tags:

javascript

csv

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

like image 476
agarwal_achhnera Avatar asked Feb 22 '23 23:02

agarwal_achhnera


2 Answers

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.
like image 99
AnthonyWJones Avatar answered Mar 05 '23 16:03

AnthonyWJones


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.

like image 27
Dmitry Ovsyanko Avatar answered Mar 05 '23 16:03

Dmitry Ovsyanko