asp.net文件下载

发布时间:2020-03-26 11:50:29 作者:Ctansuozhe
来源:网络 阅读:353
 
switch (Convert.ToInt32(Request["type"]))
{
                               //流方式下载 方法一
case 1:
string filename = "123.pdf";
string filepath = Server.MapPath("upload/123.pdf");
FileStream fs = new FileStream(filepath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "p_w_upload; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.Default));
Response.BinaryWrite(bytes);
Response.Flush();
break;
case 2://方法2
Response.ContentType = "application/x-zip-compressed";
Response.AddHeader("Content-Disposition", "p_w_upload;filename=z.zip");
string filename2 = Server.MapPath("upload/aaa.zip");
Response.TransmitFile(filename2);
break;
case 3://方法三
string fileName = "123.pdf";
string filePath = Server.MapPath("upload/123.pdf");
FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "p_w_upload;filename=" + fileName);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
break;
case 4://分块下载,方法四
string fileName4 = "123.pdf";
string filePath5 = Server.MapPath("upload/123.pdf");
 
System.IO.FileInfo fileInfo4 = new System.IO.FileInfo(filePath5);
 
if (fileInfo4.Exists == true)
{
const long ChunkSize = 1024000;//每次读取100K
byte[] buffer = new byte[ChunkSize];
 
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath5);
long dataLengthToRead = iStream.Length;//获取文件长度
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "p_w_upload; filename=" + HttpUtility.UrlEncode(fileName4));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
break;
default:
Response.Write("ddd");
break;
}
Response.End();
 
 
下载文件方法封装,怎样根据文件的扩展名下载文件(如没有扩展名,文件打开有可能出现乱码)
void DownloadFile(string filename)
{
 
string saveFileName = "123.xls";
int intStart = filename.LastIndexOf("\\") + 1;
saveFileName = filename.Substring(intStart, filename.Length - intStart);
 
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
string fileextname = fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey, fileextkey;
string filecontenttype;
try
{
regkey = Registry.ClassesRoot;
fileextkey = regkey.OpenSubKey(fileextname);
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
 
 
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
 
Response.AppendHeader("Content-Disposition", "p_w_upload;filename=" + saveFileName);
Response.ContentType = filecontenttype;
 
Response.WriteFile(filename);
Response.Flush();
Response.Close();
 
Response.End();
}
推荐阅读:
  1. 文件下载漏洞
  2. php如何实现文件下载

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

文件下载、asp.net文件下载、流下载 ne asp.ne

上一篇:选择香港服务器需要注意哪些?

下一篇:香港服务器有哪些因素影响您的网站?

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》