在IHttpHandler中控制下载文件的类型和名称
public void ProcessRequest(HttpContext context)
{
string path = context.Request.QueryString["path"];
string name = context.Request.QueryString["name"];
if(path == null || path.Trim() == string.Empty)return;
path = path.Trim();
if(name == null || name.Trim() == string.Empty)return;
if(Path.GetExtension(name).ToLower() != ".xls")
{
name += ".xls";
}
path = context.Request.ApplicationPath + "/Excel/" + path;
try
{
context.Response.WriteFile(path, false);
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("Content-disposition", "inline; filename=" + name);
}
catch(Exception ex)
{
context.Response.Write(ex.Message);
}
}
{
string path = context.Request.QueryString["path"];
string name = context.Request.QueryString["name"];
if(path == null || path.Trim() == string.Empty)return;
path = path.Trim();
if(name == null || name.Trim() == string.Empty)return;
if(Path.GetExtension(name).ToLower() != ".xls")
{
name += ".xls";
}
path = context.Request.ApplicationPath + "/Excel/" + path;
try
{
context.Response.WriteFile(path, false);
context.Response.ContentType = "application/vnd.ms-excel";
context.Response.AddHeader("Content-disposition", "inline; filename=" + name);
}
catch(Exception ex)
{
context.Response.Write(ex.Message);
}
}
附:MIME类型
csv | application/vnd.ms-excel |
css | text/css |
js | text/javascript |
doc | application/msword |
gif | image/gif |
bmp | image/bmp |
htm | text/html |
html | text/html |
jpeg | image/jpeg |
jpg | image/jpeg |
application/pdf | |
png | image/png |
ppt | application/vnd.ms-powerpoint |
rtf | application/msword |
txt | text/plain |
xls | application/vnd.ms-excel |
xml | text/xml |
wmv | video/x-ms-wmv |
wma | video/x-ms-wmv |
mpeg | video/mpeg |
mpg | video/mpeg |
mpa | video/mpeg |
mpe | video/mpeg |
mov | video/quicktime |
qt | video/quicktime |
avi | video/x-msvideo |
asf | video/x-ms-asf |
asr | video/x-ms-asf |
asx | video/x-ms-asf |
swf | application/x-shockwave-flash |