关于这方面的文章其实网上很多,有时候大家都会用到,在这里做一下汇总。
不全之处还请大家提出,多多留言阿......
一、在IE中直接打开指定的文件
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
switch(type)
{
case "jpg":
Response.ContentType = "image/JPEG";
break;
case "bmp":
Response.ContentType = "image/BMP";
break;
case "xls":
Response.ContentType = "application/vnd.ms-excel";
break;
case "pdf":
Response.ContentType = "application/pdf";
break;
case "word":
Response.ContentType = application/msword";
break;
case "powerpoint":
Response.ContentType = "application/vnd.ms-powerpoint";
break;
case "text":
Response.ContentType = "text/plain";
break;
case "html":
Response.ContentType = "text/html";
break;
default:
Response.ContentType = "octet-stream";
Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEncode(FileName));
break;
}
Response.OutputStream.Write(bytes,0,count);//其中bytes是文件的二进制流
Response.End();
二、下载文件(其实与直接打开很类似啦)
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEnco(FileName)); //其中filename要根据不同文件拼接不同后缀(通过switch判断,filename+=".xls")
Response.OutputStream.Write(bytes,0,count);//其中bytes是文件的二进制流
Response.End();
不全之处还请大家提出,多多留言阿......
一、在IE中直接打开指定的文件
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
switch(type)
{
case "jpg":
Response.ContentType = "image/JPEG";
break;
case "bmp":
Response.ContentType = "image/BMP";
break;
case "xls":
Response.ContentType = "application/vnd.ms-excel";
break;
case "pdf":
Response.ContentType = "application/pdf";
break;
case "word":
Response.ContentType = application/msword";
break;
case "powerpoint":
Response.ContentType = "application/vnd.ms-powerpoint";
break;
case "text":
Response.ContentType = "text/plain";
break;
case "html":
Response.ContentType = "text/html";
break;
default:
Response.ContentType = "octet-stream";
Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEncode(FileName));
break;
}
Response.OutputStream.Write(bytes,0,count);//其中bytes是文件的二进制流
Response.End();
二、下载文件(其实与直接打开很类似啦)
Response.Clear();
Response.Buffer = true;
Response.Charset = "utf-8";
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition","attachment; filename=" + HttpUtility.UrlEnco(FileName)); //其中filename要根据不同文件拼接不同后缀(通过switch判断,filename+=".xls")
Response.OutputStream.Write(bytes,0,count);//其中bytes是文件的二进制流
Response.End();
浙公网安备 33010602011771号