亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

asp.net文件下載

發布時間:2020-03-26 11:50:29 來源:網絡 閱讀:353 作者:Ctansuozhe 欄目:編程語言
 
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();
}
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

广饶县| 临桂县| 宕昌县| 平泉县| 呼和浩特市| 安义县| 耿马| 延寿县| 菏泽市| 雷山县| 河曲县| 延川县| 鞍山市| 宜丰县| 柳江县| 卫辉市| 安远县| 济源市| 康定县| 新余市| 即墨市| 巴彦县| 策勒县| 邛崃市| 华池县| 卢氏县| 类乌齐县| 商都县| 营山县| 梅州市| 抚顺县| 海门市| 罗平县| 昭平县| 营山县| 襄樊市| 石屏县| 丹凤县| 棋牌| 辽中县| 太仓市|