日期:2014-05-20 浏览次数:20440 次
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { try { //Session["userid"] = "adminTest"; //下载文件页面不能用session string userid = ""; userid = Request.QueryString["uid"].ToString().Trim(); string FileGuid = Request.QueryString["id"].ToString().Trim(); Guid guid = new Guid(FileGuid); string FileServerPath = ""; FileServerPath = bllfile.GetFilePath(userid, guid).ServerPath; if (FileServerPath == "") { Response.Write("<font size=3 color=red>没有找到文件!</font>"); } else { if (!SendFile(FileServerPath)) { Response.Write("<font size=3 color=red>下载出错,请与管理员联系!</font>"); } else { int k = 0; k = k + bllfile.Update(guid); //这里是去更新文件下载次数+1,但是文件越大下载的时间越长,这里就会多次执行,为什么,而不是只执行一次 if (k < 1) { Response.Write("<font size=3 color=red>更新您的下载次数失败</font>"); } } } catch (Exception ex) { Common.LogManager.LogWrite("Inner_ResourceList_DownFile-->Page_Load", ex.ToString()); Response.Write("<font size=3 color=red>对不起,无法下载文件!</font>"); } Response.End(); } } private bool SendFile(string filename) { bool check; try { Response.Clear(); Response.ClearHeaders(); FileInfo f = new FileInfo(filename); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlPathEncode(f.Name)); Response.TransmitFile(filename); check = true; } catch (Exception ex) { check = false; } return check; }