asp.net文件下载
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();
}