|
楼主 |
发表于 2020-7-10 01:15:01
|
显示全部楼层
protected void Button1_Click(object sender, EventArgs e)
{
if (Request["id"] != null)
{
int id = Convert.ToInt32(Request["id"]);//转换为数字,防止sql注入
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString);
conn.Open();
adr = "select 附件上传地址 from Mail WHERE 邮件编号 = " + id;
SqlCommand cmd = new SqlCommand(adr, conn);
string path = cmd.ExecuteScalar().ToString();
//取得路径
filepath = FormatString(path.ToString()).ToString().Trim();
filepath = filepath.Substring(0, filepath.Length - 1);//去掉最末尾的分號
string[] strpaths = filepath.Split(';');//把分號之間的路徑取出來放到數組裡
for (int i = 0; i < strpaths.Length; i++)
{
string Temp_filename = FormatString(path.ToString()).ToString();
int pos = Temp_filename.LastIndexOf("\\") + 1;
filename = Temp_filename.Substring(pos, Temp_filename.Length - pos).ToString();
filename = HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename));
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
FileStream fs = new FileStream(filepath, FileMode.Open);
long FileSize = fs.Length;
byte[] Buffer = new byte[(int)FileSize];
fs.Read(Buffer, 0, (int)fs.Length);
fs.Close();
Response.ContentType = "application/octe-stream";
Response.AddHeader("content-disposition", "attachment;filename=" + filename);
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.BinaryWrite(Buffer);
conn.Close();
}
}
} |
|