Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends ...

i m creating an excel file using Microsoft.Office.Interop.Excel in ASP.NET. While at Developing time, An Excel File was generating and downloading properly. But After hosting the project on IIS, I am getting following error

Access to the path 'C:\Mori-2014\Report\DailyReport.xls' is denied.

C:\Mori-2014\Report\DailyReport.xls is path where the excel file is generating. and from there data is converted into binary format and generating downloadable file.

C#
string FilePath="C:\Mori-2014\Report\DailyReport.xls";
byte[] bytes = System.IO.File.ReadAllBytes(FilePath);
Response.Buffer = true;
Response.Clear();
Response.ClearHeaders();
Response.ContentType = "application/vnd.ms-excel";
Response.CacheControl = "public";
Response.AddHeader("Pragma", "public");
Response.AddHeader("Expires", "0");
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
Response.AddHeader("Content-Description", "Excel File Download");
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
Response.BinaryWrite(bytes);

Response.Flush();
Response.End();


Please Help me.
Posted
Updated 15-Dec-14 22:29pm
v2
Comments
Sinisa Hajnal 16-Dec-14 5:19am    
Does it exist on your server? In general it is very bad design to use fixed (hard-coded) setting for anything. Use database setting, config file or whatever to have the ability to change the target folder without recompiling the code.
Sinisa Hajnal 16-Dec-14 5:23am    
Does it exist on your server?

1 solution

First of all if you are creating/downloading any file, don't use direct C: drive path it would always give you Access denied after hosting in IIS.

So the better option is use :
C#
string FilePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Path on drive would be like "C:\Users\user_name\AppData\Roaming".
You can create you folder in this path and access as per you projects name.

Store/Download/Manipulate your File(Excel file) in this location which is user specific.
 
Share this answer
 
v2

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