Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Good afternoon,

Implemented a method in a web application, which receives the full path where the file of any length, is located in the data server, mapped to a network drive, which checks if the file exists and if so by forcing open a window download. Here is the code:
//A cell of the grid selected returns the string "Z:\Test\Apae Analytical Balance.pdf"
FileInfo file = new FileInfo (grvConsulta.SelectedRow.Cells [17].Text);

                          if (file.Exists)
                         {
                             Response.Clear();
                             Response.AddHeader ("Content-Disposition", "attachment; filename =" + file.Name);
                             Response.AddHeader ("Content-Length ", file.Length.ToString());
                             Response.ContentType = "application / octet-stream";
                             Response.Flush();
                             Response.WriteFile (file.FullName);
                             Response.End();
                         }
                         else
                         {
                             ScriptManager.RegisterStartupScript (this, this.GetType (), "ExibirMensagem", "alert ('" + HttpUtility.HtmlDecode ("File not located in the know.") + "');", true);
                         }

The problem is that localhost running the application, the file is located and requested to download normally, but after publishing the site in IIS from a test server, the file is not found, and the IIS server is not the same database server and is with the mapping data server correct. 've tried adding a key in the web.config with the Data directory, but the problem persists.
Any ideas to solve this problem?

Thank you in advance.
Posted
Updated 12-Apr-11 1:26am
v2
Comments
Toniyo Jackson 12-Apr-11 7:27am    
Added pre tag

1 solution

Have a read through this article which describes some best practices and problems you may run into

http://support.microsoft.com/kb/207671[^]

Do not use drive letters mapped to network shares. Not only are there only 26 potential driver letters to select from, but if you try to use a drive letter that is mapped in a different security context, problems can occur. Instead, you must always use Universal Naming Convention (UNC) names to access resources. The format must look similar to the following: \\MyServer\filesharename\directoryname\filename


Mapped drives are user-profile based, so unless the security context you are running your IIS application as on the test server explicity has this drive mapping, it's not going to understand what Z:\ is.
 
Share this answer
 
Comments
Figueiredo André 12-Apr-11 8:30am    
Hi Dylan,

I set a key in the web.config file with the path \\MyServer\filesharename\directoryname and just add the file name for the procedure below:

string fileName = grvConsulta.SelectedRow.Cells[17].Text.Split(':')[1];

string fileServerBasePath = ConfigurationManager.AppSettings["FileServerBasePath"];

if (!string.IsNullOrEmpty(fileServerBasePath))
{

FileInfo file = new FileInfo (fileServerBasePath + fileName);

if (file.Exists)
{
/ / Clears the current output buffer
Response.Clear ();
/ / Adds a header that specifies the default name for the dialog box Save As ...
Response.AddHeader ("Content-Disposition", "attachment; filename =" + file.Name);
/ / Add the header file size so the browser can display the download progress
Response.AddHeader ("Content-Length ", file.Length.ToString ());
Response.ContentType = "application / octet-stream";
Response.Flush ();
Response.WriteFile (file.FullName);
Response.End ();
}
else
{
ScriptManager.RegisterStartupScript (this, this.GetType (), "ExibirMensagem", "alert ('" + HttpUtility.HtmlDecode ("File not located in the know.") + "');", true);
}
}
else
{
ScriptManager.RegisterStartupScript (this, this.GetType (), "ExibirMensagem", "alert ('" + HttpUtility.HtmlDecode ("The configured directory is invalid.") + "');", true);
}

But the problem persists, select the item to open the attachment, it is not found.
Dylan Morley 12-Apr-11 8:37am    
What security context is your application running under?

e.g. are you running as MACHINE\ASPNET account? If so, that account needs permissions to the drive you are trying to access

Read here..

http://weblogs.asp.net/mschwarz/archive/2003/03/31/4515.aspx

If you are running IIS6 or later, you could create an application pool under an account that has access to this location, then run your asp.net application within that pool. It should then be able to access the resource.

Accessing network resources from IIS \ ASP.Net is going to be inherently difficult because of security considerations. If you allow access as above, you are opening a security hole.

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