Asp.net實現附件下載(亂碼解決)

來源:互聯網
上載者:User
代碼

    private void Page_Load(object sender, System.EventArgs e)
{
if (Request.QueryString["Recordid"] != null && Request.QueryString["Recordid"].ToString() != "")
{
StarTech.DBUtility.AdoHelper adoHelper = StarTech.DBUtility.AdoHelper.CreateHelper("News_Instance");
System.Data.DataTable resourceDs = adoHelper.ExecuteSqlDataset("Select * From t_news_resource Where Recordid='" + Request.QueryString["Recordid"].ToString() + "'").Tables[0];
if (resourceDs.Rows.Count > 0)
{
string filepath = this.MapPath(resourceDs.Rows[0]["path"].ToString());
byte[] ByteFile = GetFileByte(filepath);
if (ByteFile != null)
{
string FileType = System.IO.Path.GetExtension(filepath);
string FileName = System.IO.Path.GetFileName(filepath);
Response.Clear();
Response.HeaderEncoding = System.Text.Encoding.Default;
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
Response.AddHeader("Content-Length", ByteFile.Length.ToString());
Response.ContentType = CheckType(FileType);
Response.BinaryWrite(ByteFile);
Response.End();
}
else
{
Response.Write("<font color=red>找不到相關附件!【<a href='javascript:window.opener = null;window.close()'>關閉</a>】</font>");
}
}
}
else
{
Response.Write("<font color=red>參數錯誤或者許可權不夠 【<a href='javascript:window.close()'>關閉</a>】</font>");
}
}
//擷取檔案二進位流
private Byte[] GetFileByte(string path)
{
try
{
System.IO.FileStream file = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read);
Byte[] imgByte = new Byte;
file.Read(imgByte, 0, imgByte.Length);
file.Close();
return imgByte;
}
catch (Exception ex)
{
string str = ex.Message;
return null;
}
}
/// <summary>
/// 根據檔案的副檔名來擷取對應的"輸出資料流的HTTP MIME"類型
/// </summary>
/// <param name="FileType"></param>
/// <returns></returns>
private string CheckType(string FileType)
{
string ContentType;
switch (FileType.ToLower())
{
case ".asf ":
ContentType = "video/x-ms-asf ";
break;
case ".avi ":
ContentType = "video/avi ";
break;
case ".doc ":
ContentType = "application/msword "; break;
case ".zip ":
ContentType = "application/zip "; break;
case ".xls ":
ContentType = "application/vnd.ms-excel "; break;
case ".gif ":
ContentType = "image/gif "; break;
case ".jpg ":
ContentType = "image/jpeg "; break;
case "jpeg ":
ContentType = "image/jpeg "; break;
case ".wav ":
ContentType = "audio/wav "; break;
case ".mp3 ":
ContentType = "audio/mpeg3 "; break;
case ".mpg ":
ContentType = "video/mpeg "; break;
case ".mepg ":
ContentType = "video/mpeg "; break;
case ".rtf ":
ContentType = "application/rtf "; break;
case ".html ":
ContentType = "text/html "; break;
case ".htm ":
ContentType = "text/html "; break;
case ".txt ":
ContentType = "text/plain "; break;
default:
ContentType = "application/octet-stream ";
break;
}
return ContentType;
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.