namespace ROHSUI.ReportCenter { //2009-7-21 上午 张锐 创建 public partial class Attachment : System.Web.UI.Page { string ReportNO; string ReportID; string HistoryPath; protected void Page_Load(object sender, EventArgs e) { this.lblMessage.Text = ""; try { if (null != Request.QueryString["ReportNO"] && null != Request.QueryString["ReportID"] && null != Request.QueryString["HistoryPath"]) { ReportNO = this.lblReportNO.Text = Request.QueryString["ReportNO"].ToString(); this.hfReportID.Value = ReportID = Request.QueryString["ReportID"].ToString(); HistoryPath = Request.QueryString["HistoryPath"].ToString(); if (null != Request.QueryString["IsView"]) { string isView = Request.QueryString["IsView"].ToString(); if (isView == "1") { this.fuEnclosure.Visible = false; this.btnDel.Visible = false; this.btnUpload.Visible = false; } } } } catch { throw new Exception(""); } } protected void btnUpload_Click(object sender, EventArgs e) { string path = ""; if (fuEnclosure.HasFile) { if (fuEnclosure.FileBytes.GetLength(0) > 4194304) { //在此仅仅是提示用户文件大小超过4MB lblMessage.Text = "文件大小不能大于4MB"; return; } DirectoryInfo uploadfolder = new DirectoryInfo(Server.MapPath("..//ReportFolder//" + ReportNO)); if (!uploadfolder.Exists) { uploadfolder.Create(); } //文件上传 try { //判断是否已经有该文件 bool existe = false;//non default foreach (FileInfo f in uploadfolder.GetFiles("*.*")) { if (f.Name.Trim() == fuEnclosure.FileName.Trim()) { existe = true; } } if (!existe) { path = "..//ReportFolder//" + ReportNO + "//" + fuEnclosure.FileName.Trim(); fuEnclosure.SaveAs(Server.MapPath(path)); AttachmentBLL attachment = new AttachmentBLL(); RoHS_Attachment rohs_attachment = new RoHS_Attachment(); rohs_attachment.AttachmentID = Guid.NewGuid().ToString(); rohs_attachment.ReportID = this.hfReportID.Value; rohs_attachment.PicPath = path; attachment.InsertAttachment(rohs_attachment); lblMessage.Text = "上传成功!"; } else { lblMessage.Text = "该文件已经存在!"; } } catch { //显示错误信息:上传文件失败 //若是数据库错误的话则对已经上传到服务器上的文件不做什么处理仅仅显示上传失 lblMessage.Text = "上传失败!"; } } else { lblMessage.Text = "上传失败!"; } ddlAttach.DataBind(); ddlAttach.SelectedIndex = -1; } protected void btnDel_Click(object sender, EventArgs e) { if (false == string.IsNullOrEmpty(ddlAttach.SelectedValue.ToString())) { string ReportID = ddlAttach.SelectedValue; ReportID = ReportID.Substring(ReportID.LastIndexOf("|") + 1); string AttachmentID = ddlAttach.SelectedValue; AttachmentID = AttachmentID.Substring(0, AttachmentID.IndexOf("|")); string truePath = "..//ReportFolder//" + ReportNO + "//" + ddlAttach.SelectedItem.Text.Trim(); AttachmentBLL attachment = new AttachmentBLL(); RoHS_Attachment rohs_attachment = new RoHS_Attachment(); rohs_attachment.AttachmentID = AttachmentID; rohs_attachment.ReportID = ReportID; rohs_attachment.PicPath = truePath; try { attachment.Delete(rohs_attachment); } catch (Exception ex) { //显示删除数据失败 lblMessage.Text = "删除失败!"; ddlAttach.DataBind(); return; } try { FileInfo fi = new FileInfo(Server.MapPath(truePath)); fi.Delete(); ddlAttach.DataBind(); hfAttch.Value = ""; } catch (Exception ex) { ddlAttach.DataBind(); } } } protected void ddlAttach_SelectedIndexChanged(object sender, EventArgs e) { string aburl; aburl = ddlAttach.SelectedValue.ToString(); string ReportID = aburl.Substring(aburl.LastIndexOf("|") + 1); AttachmentBLL attachment = new AttachmentBLL(); List<RoHS_Attachment> attachments = attachment.GetAttachmentByReportID(ReportID); if (false == string.IsNullOrEmpty(aburl)) { foreach (RoHS_Attachment att in attachments) { if (att.AttachmentID == ddlAttach.SelectedValue) { string[] url; int length; aburl = ""; url = att.PicPath.Split('//'); length = url.GetLength(0); for (int j = 0; j < length; j++) { if (".." != url[j] && "." != url[j]) { aburl += "/" + url[j]; } } aburl = "~/" + aburl; aburl = VirtualPathUtility.ToAbsolute(aburl); aburl = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + aburl; hfAttch.Value = aburl; } } } } protected void ddlAttach_DataBound(object sender, EventArgs e) { for (int i = 0; i < ddlAttach.Items.Count; i++) { ddlAttach.Items[i].Text = ddlAttach.Items[i].Text.Substring(ddlAttach.Items[i].Text.LastIndexOf("//")+1); } ListItem listItem = new ListItem(); listItem.Text = "please select"; listItem.Value = ""; ddlAttach.Items.Insert(0,listItem); //ddlAttach.SelectedIndex = ddlAttach.Items.Count - 1; ddlAttach.SelectedIndex = - 1; } protected void Button2_Click(object sender, EventArgs e) { if (false == string.IsNullOrEmpty(ddlAttach.SelectedValue.ToString())) { string filename = ddlAttach.SelectedItem.Text; string truepath = "..//ReportFolder//" + ReportNO + "//" + ddlAttach.SelectedItem.Text.Trim(); string[] url; int length; string aburl = ""; url = truepath.Split('//'); length = url.GetLength(0); for (int j = 0; j < length; j++) { if (".." != url[j] && "." != url[j]) { aburl += "/" + url[j]; } } aburl = "~/" + aburl; //获取真正的路径 FileInfo fi = new FileInfo(Server.MapPath(truepath)); if (fi.Exists) { HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(filename)); HttpContext.Current.Response.ContentType = "application/ms-excel";// 指定返回的是一个不能被客户端读取的流,必须被下载 HttpContext.Current.Response.WriteFile(aburl); // 把文件流发送到客户端 HttpContext.Current.Response.End(); } else { lblMessage.Text = "文件不存在"; } } } protected void return_Click(object sender, EventArgs e) { Response.Redirect(HistoryPath); } } }