Lamloumi Afif
Lamloumi Afif

Reputation: 9081

Not Supported exception in the download of file with c#

I'd like to download a file in my ASP.NET application. I used this snippet:

try
{
    string s = fichier.GetFichierUrlById(_id);
    Response.ContentType = "application/" + Path.GetExtension(s); 
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + s);
    Response.TransmitFile(Server.MapPath("~/Downloads/"+s));
    Response.End();
}
catch { }

In this example, I have the file setup.exe, and I get an exception like The format of the given path is not supported.

What is the cause of this exception? How can I fix the code?

Upvotes: 0

Views: 253

Answers (2)

Simon Halsey
Simon Halsey

Reputation: 5469

TransmitFile would assume that there is a physical file in the downloads sub folder.

If that is not the case, then you'll need to do something like write the data into the output stream yourself.

Upvotes: 1

Uatec
Uatec

Reputation: 141

Check the value of S, and hence, the return value of "Server.MapPath("~/Downloads/"+s).

You might have some invalid characters in S which blow things out maybe?

Upvotes: 2

Related Questions