Pregunta

I've taken on an asp/c# web app to fix originally done by the previous developer at my workplace. The code shows a Gridview populated by results from a query showing a list of files, one column is made up of 'command fields' that when clicked download a file. Everything seems to go smoothly until it reaches the file download as it can't seem to find the file on the server. My C# really isn't strong so bear with me and if you need further info that I've missed out please do say so.

Here is the specific part of code that causes problems:

//strSuppDocName - is already declared elsewhere
string path = System.IO.Path.Combine(Server.MapPath("~/Documents/"), strSuppDocName);

if (!Directory.Exists(path)){
                System.Windows.Forms.MessageBox.Show(path + " - file path doesn't exist");
            }
            else {
                System.Net.WebClient client = new System.Net.WebClient();
                Byte[] buffer = client.DownloadData(path);

                if (buffer != null)
                {
                    Response.ClearContent();
                    Response.ClearHeaders();
                    FileInfo file = new FileInfo(path);
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "Attachment;FileName:" + file.Name);
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = ReturnExtension(strExtSuppDoc.ToLower());

                    Response.WriteFile(file.FullName);
                    Response.End();
                }
            }

What happens when I run the code is that the grid view populates okay, I click the file to download and it enters the first branch of the if statement showing the path. Before I added in the if statement it was showing the following error: "could not find a part of the path". I've tried fiddling with the path such as setting it absolutely:

string path = System.IO.Path.Combine(@"E:\web\Attestation\Documents\", strSuppDocName);

And without using the Combine method above and using standard string concatenation with '+'. Any help or guidance is most appreciated, thanks!

¿Fue útil?

Solución 3

The answer in short is that the file name was incorrect.

Strangely or mistakenly the author of the code, when uploading a given file, added an extra extension so a file would be something like 'image.png' to start off with then when uploaded would become image.png.png. Why didn't I notice this before you may ask? Simply because the whole path wasn't shown in Windows XP (don't ask why I was using XP) when viewing it through the explorer window and I dismissed this issue long before - a big mistake! After trying to find the file by typing the address of the file into the windows explorer address bar and receiving an error that the file doesn't exist, yet I could plainly see it did, a colleague looked at for the file remotely using Windows 7 and we saw that the file was shown as 'image.png.png'. Thereafter the path to the file worked correctly.

Otros consejos

You're mixing a handful of technologies here. First of all, this doesn't belong in a web application:

System.Windows.Forms.MessageBox.Show(path + " - file path doesn't exist");

Web applications aren't Windows Forms applications. This won't display anything to someone using the web application, because there's no concept of a "message box" over HTTP.

More to the point, however, you're using path in two very different ways. Here:

Byte[] buffer = client.DownloadData(path);

and here:

FileInfo file = new FileInfo(path);

Is path a URL on the network or a file on the file system? It can't be both. The first line is treating it as a URL, trying to download it from a web server. The second line is treating it as a local file, trying to read it from the file system.

What is path and how are you looking to access it? If it's a URL, download it with the WebClient and stream it to the user. If it's a file, read it from the file system and stream it to the user. You can't do both at the same time.

If you are interacting with a path on a network (aka UNC path), you have to use Server.MapPath to turn a UNC path or virtual path into a physical path that .NET can understand. So anytime you're opening files, creating, updating and deleting files, opening directories and deleting directories on a network path, use Server.MapPath.

Example:

System.IO.Directory.CreateDirectory(Server.MapPath("\\server\path"));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top