标签:code http ext com 文件 string
if (Request["name"] != null)
{
try
{
string FileName = MyCrypt.Decrypt(Request["name"]);
//string FilePath = Server.MapPath(string.Format("UploadFile/{0}", FileName));
string strPhyPath = Server.MapPath(string.Format("UploadFile/{0}", FileName));//fileurl是文件的相对地址
if (File.Exists(strPhyPath))
{
//取文件大小
FileStream MyFileStream;
uint FileSize1;
MyFileStream = new FileStream(strPhyPath, FileMode.Open, FileAccess.Read, FileShare.None);
int iConverTemp = Convert.ToInt32(MyFileStream.Length);
FileSize1 = (uint)(iConverTemp);
MyFileStream.Close();
//存在,下载
Page.Response.ContentType = "APPLICATION/OCTET-STREAM";
Page.Response.AddHeader("Content-length", FileSize1.ToString());//下载文件长度
Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
Page.Response.WriteFile(strPhyPath);
Response.Flush();
Response.End();
}
else
{
//MessageBox.Show("该附件不存在或者已被删除,请联系管理员处理!", this);
Response.Write("<script>alert(‘该附件不存在或者已被删除,请联系管理员处理!‘);if(window.parent!=window) window.parent.close(); else {window.opener=null;window.open(‘,‘,‘_self‘);window.close();}</script>");
return;
}
//第二种下载方法
////Response.ContentType = "application/x-zip-compressed"; itjeff modified.
//Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)));
//Response.TransmitFile(FilePath);
//第三种下载方法
//Response.ContentType = "application/ms-winword";
//Response.ContentEncoding = System.Text.Encoding.Default;
//Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
//Response.WriteFile(FilePath);
//Response.End();
//第四种下载方法
////以字符流的形式下载文件
//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", "attachment; filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
//Response.BinaryWrite(bytes);
//Response.Flush();
//Response.End();
}
catch (Exception er)
{
Response.Write(string.Format("<script>alert(‘下载附件时发生异常,异常信息为:{0}‘);</script>", er.Message));
}
}
标签:code http ext com 文件 string
原文地址:http://www.cnblogs.com/itjeff/p/3786160.html