Click here to Skip to main content
15,887,262 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In an ASP.NET MVC app, I am trying to read data from Excel file, but I get this error:

The error appears in production mode, but in development mode, there's no problem.

I am making host on Local Host on my computer:

Microsoft Excel cannot access the file 'C:\Users\PC\Desktop\qqq\Content\Uploads\dataFile.xls'.

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.


What I have tried:

I tried to check the file, it is not used by another program.
Posted
Updated 4-Oct-23 3:16am
v3

It works in development because your machine is both the server and the client at the same time.

This will not work in production because code running on the server, and any code running in the browser, will not have any access to the client filesystem, for security reasons.

If you want your server-side code to work with the file, you will have to provide some way of uploading the Excel file to the server.
 
Share this answer
 
Comments
MrLonely_2 3-Oct-23 14:40pm    
The Excel file is uploaded successfully to the server but the problem appear when I try to open file
Dave Kreskowiak 3-Oct-23 15:22pm    
Well, you have to check the path the uploaded file is written to, check the code that is writing the file to disk to make sure the file is closed AND THE STREAM OBJECT DISPOSED, then make sure the filepath of the file is correct when you pass that path to the code that is opening the file to read it.

I would suggest instrumenting your code to log all of this data to a file so you can see what's going on in the production environment.
Hi Mate

you need to use Server.MapPath when reading from localhost , check following code hope it helps

if (!Directory.Exists(Server.MapPath("~/App_Data/" + Market)))
        {
            Directory.CreateDirectory(Server.MapPath("~/App_Data/" + Market));
        }




above code works for me

Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900