How to open csv file in IE via javascript

On my asp page, I need to open the csv file in IE using java script. The code I use is as follows:

 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 ");

The code is running on the IIS server.

When I run this code and open the 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 the file is being created. This path is correct, and I think that this file is also not used by another program

Please help me what should I do

+4
source share
2 answers

The problem is that when you open Excel, it will try to extract the CSV file itself, this is a change in behavior in office applications since 2007. However, since Excel is running in a different process, it will not send cookies that would have been created at login time. When the website receives the request, it will respond with a status of 401.

There are no simple solutions that I know with quite satisfactory results. Here are a few solutions, but all have flaws.

  • Make authentication cookies persistent, which will allow Office applications to collect and send cookies. The downside being the user remains constantly logged even after the client machine reboots (similar to how Stackoverflow works).
  • Use a standard HTTP authentication protocol, such as Basic or Negotiate. The downside is that this will cause Excel to display the login window, and the user must log in again. The only exception to this drawback is the use of "Negotiate" or "NTLM" against the IIS window, where the site is registered as part of the IE intranet zone, in which case the HTTP stack used by excel will try to use the current user credentials.
  • On the server side, a script that can anonymously send the csv file and include a unique identifier (such as a GUID) in the URL, which is a permission grant. Much harder to set up.
+5
source

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

If you just want to show CSV text in a browser window, it is probably best to change its extension or make some proxy page with Content-Type=text/plain and not mention CSV at all. The CSV / Excel Association seems to be hard coded in MSIE.

+3
source

Source: https://habr.com/ru/post/1389220/


All Articles