1

I am using ASP.Net Webforms and C# to download a file which I stored at a physical path. (C:\Users\Public\Temp). Since I did not store the file on the server, I am using a physical location to download the file using the following code snippet from the .aspx handler

        HttpRequest request = System.Web.HttpContext.Current.Request;
        HttpResponse response = System.Web.HttpContext.Current.Response;
        String fileName = request.QueryString["fileName"];
        String storagePath = "C:/Users/Public/Temp/";
        String filePath = String.Format("{0}/{1}", storagePath, fileName);
        response.ClearContent();
        response.Clear();
        response.ContentType = "application/octet-stream";
        response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.TransmitFile(filePath);
        response.Flush();
        response.End();

I got the reference from the post here. But, when I initiate the download through a button click, I get the

"Could not find part of the part C:/Users/Public/Temp/"

message on the exception. I am not sure if this is because of the folder not being on the server. Is there anything I need to add to the file path? Please help me understand where I am going wrong. Please do suggest if there is a better way of doing the downloads.

(This is a prototype I am working on which I will extend to a network location)

Community
  • 1
  • 1
quirkystack
  • 1,387
  • 2
  • 17
  • 42

1 Answers1

1

I figured this was because of the fileName query string being empty (due to postbacks). Pretty lame I didn't find it before. In this case , the TransmitFile was getting a path to the "C:/Users/Public/Temp/" but not the actual file path and hence the error.

Now, I persist the data for fileName using ViewState and TransmitFile gets the full path C:/Users/Public/Temp/fileName and it works fine.

quirkystack
  • 1,387
  • 2
  • 17
  • 42