Document using hyperlinks directly designated to be downloaded

Download file
- "file using a hyperlink to download specified directly
be browser parses the text is displayed
can not be parsed file browser will be downloaded
-" Implementation: regardless of the file format, do not use a browser to display the download is complete
point to general processing program file address as a parameter
modification response header: ContentType = "application / octet- stream";
setting header: AddHeader ( "Content-Disposition" , "attachment; filename = \" filename \ ";");
output file: context.Response.WriteFile (file address);
- "Tip: If the Chinese file name garbled, can be url encoded
HttpUtility.UrlEncode (filename, System.Text.Encoding.UTF8);
returns a string as a file first name

 

public class Download : IHttpHandler
{

void ProcessRequest public (the HttpContext context)
{
// Tip: If Chinese file name garbled, can be url encoded
//HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);
// Returns a string as a file name

string f = context.Request [ "f" ];

context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=\""+f+"\";");//%ab%12

context.Response.WriteFile(AppDomain.CurrentDomain.BaseDirectory+f);
}

public bool IsReusable
{
get
{
return false;
}
}
}

Guess you like

Origin www.cnblogs.com/jiangyunfeng/p/11122478.html