protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Request.QueryString["Flag"] == "download") { if (!IsNumber(Request.QueryString["ID"])) { Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('查询字符串错误');window.close();</script>"); return; } LabMS.BLL.AttachmentItem attachItem = new LabMS.BLL.AttachmentItem(); LabMS.Model.AttachmentItem attachmodel = new LabMS.Model.AttachmentItem(); attachmodel = attachItem.GetModel(int.Parse(Request.QueryString["ID"].Trim())); FileItemDownload(attachmodel.ID, attachmodel.Item_Address, attachmodel.Item_Name); return; } else if (!string.IsNullOrEmpty(Request.QueryString["ID"]) && Request.QueryString["Flag"] == "attachdownload") { if (!IsNumber(Request.QueryString["ID"])) { Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('查询字符串错误');window.close();</script>"); return; } LabMS.BLL.Attachment Attach = new LabMS.BLL.Attachment(); LabMS.Model.Attachment attachmodel = new LabMS.Model.Attachment(); attachmodel = Attach.GetModel(int.Parse(Request.QueryString["ID"].Trim())); FileItemDownload(attachmodel.ID, attachmodel.Attach_Address, attachmodel.Attach_Name); return; } if (!string.IsNullOrEmpty(Request.QueryString["ID"])) { if (!IsNumber(Request.QueryString["ID"])) { Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('查询字符串错误');window.close();</script>"); return; } LabMS.BLL.Attachment Attach = new LabMS.BLL.Attachment(); LabMS.Model.Attachment attachmodel = new LabMS.Model.Attachment(); attachmodel = Attach.GetModel(int.Parse(Request.QueryString["ID"].Trim())); Gv_DataBind(attachmodel); } } //if (Request.QueryString["ID"] == null) //{ // Common.JShelper.JSAlert(Page, "err", "参数错误!"); // return; //} //else if (Request.QueryString["ID"] != null && Request.QueryString["type"] == null) //{ // int iTemp = 0; // string strID = Request.QueryString["ID"].ToString(); // if (!int.TryParse(strID, out iTemp)) // { // Common.JShelper.JSAlert(Page, "err", "参数错误!"); // return; // } // LabMS.Model.Attachment mattach = new LabMS.Model.Attachment(); // LabMS.BLL.Attachment battach = new LabMS.BLL.Attachment(); // mattach = battach.GetModel(iTemp); // if (mattach.Attach_Name == "") // { // Response.Redirect("DownloadFile.aspx?ID=" + strID + "&type=1"); // } // else // { // string FullFileName = mattach.Attach_Address; // string FileName = mattach.Attach_Name; // try // { // FullFileName = Server.MapPath("../" + FullFileName); // Context.Response.Buffer = true; // Context.Response.Clear(); // Context.Response.AppendHeader("Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(FullFileName, System.Text.Encoding.UTF8)); // Response.ContentType = "application/x-zip-compressed"; // Response.TransmitFile(FullFileName); // } // catch // { // Common.JShelper.JSAlert(Page, "err", "路径错误!"); // return; // } // Response.End(); // } //} //else if (Request.QueryString["ID"] != null && Request.QueryString["type"] != null) //{ // if (Request.QueryString["type"].ToString().Equals("1")) // { // List<LabMS.Model.AttachmentItem> listattchitem = new List<LabMS.Model.AttachmentItem>(); // LabMS.BLL.AttachmentItem battachitem = new LabMS.BLL.AttachmentItem(); // int iTemp = 0; // string strID = Request.QueryString["ID"].ToString(); // if (!int.TryParse(strID, out iTemp)) // { // Common.JShelper.JSAlert(Page, "err", "参数错误!"); // return; // } // listattchitem = battachitem.GetModelList("Attach_ID = " + strID); // foreach (LabMS.Model.AttachmentItem mai in listattchitem) // { // mai.Item_Address = "<a onclick=foo('DownloadFile.aspx?type=2&ID=" + mai.ID.ToString() + "')>点击下载</a>"; // } // gvFile.DataSource = listattchitem; // gvFile.DataBind(); // gvFile.Visible = true; // } // else if (Request.QueryString["type"].ToString().Equals("2")) // { // int iTemp = 0; // string strID = Request.QueryString["ID"].ToString(); // if (!int.TryParse(strID, out iTemp)) // { // Common.JShelper.JSAlert(Page, "err", "参数错误!"); // return; // } // LabMS.Model.AttachmentItem mattach = new LabMS.Model.AttachmentItem(); // LabMS.BLL.AttachmentItem battach = new LabMS.BLL.AttachmentItem(); // mattach = battach.GetModel(iTemp); // string FullFileName = mattach.Item_Address; // string FileName = mattach.Item_Name; // try // { // FullFileName = Server.MapPath("../" + FullFileName); // Context.Response.Buffer = true; // Context.Response.Clear(); // Context.Response.AppendHeader("Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); // Response.ContentType = "application/x-zip-compressed"; // Response.TransmitFile(FullFileName); // } // catch // { // Common.JShelper.JSAlert(Page, "err", "路径错误!"); // return; // } // Response.End(); // } //} }
private void FileItemDownload(int itemID) { if (itemID == 0) { lb_Error.Text = "������������"; return; } System.IO.Stream iStream = null; try { //�����ļ�ȫ·���� LabMS.BLL.AttachmentItem AttachItem = new LabMS.BLL.AttachmentItem(); LabMS.Model.AttachmentItem itemmodel = new LabMS.Model.AttachmentItem(); itemmodel = AttachItem.GetModel(itemID); string fullpath = Server.MapPath("../" + itemmodel.Item_Address); string fileName = itemmodel.Item_Name; string NameTemp = ""; if (Request.Browser.Browser == "IE") { NameTemp = HttpUtility.UrlEncode(fileName); NameTemp = NameTemp.Replace("+", "%20"); } else { NameTemp = fileName; NameTemp = NameTemp.Replace(" ", ""); } Response.Clear(); Response.Charset = "UTF-8"; Response.Buffer = true; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.HeaderEncoding = System.Text.Encoding.UTF8; Response.AppendHeader("Content-Disposition", "attachment;filename=" + NameTemp); Response.ContentType = "application/octet-stream"; byte[] buffer = new byte[100000]; int iLength; long dataToRead; iStream = new FileStream(fullpath, FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read); dataToRead = iStream.Length; while (dataToRead > 0) { if (Response.IsClientConnected) { iLength = iStream.Read(buffer, 0, 100000); Response.BinaryWrite(buffer); Response.Flush(); buffer = new byte[100000]; dataToRead = dataToRead - iLength; } else { dataToRead = -1; } } } catch (Exception ex) { lb_Error.Text = ex.Message; } finally { if (iStream != null) { iStream.Close(); } } }