protected void gv_List_Attachment_RowDeleting(object sender, GridViewDeleteEventArgs e) { int id = (int)gv_List_Attachment.DataKeys[e.RowIndex]["ID"]; EWF_Task_AttachmentBLL att = new EWF_Task_AttachmentBLL(id); if (att.Model.UploadStaff == (int)Session["UserID"]) { att.Delete(); BindAttachmentList(); } else { MessageBox.Show(this, "对不起,您只能删除您自己上传的附件!"); } }
//Upload Attachment protected void btn_Up_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { try { string fullfilename = FileUpload1.FileName; if (fullfilename.LastIndexOf(".") < 0) { MessageBox.Show(this, "无法识别的附件格式!"); return; } int FileSize = (FileUpload1.PostedFile.ContentLength / 1024); if (FileSize > ConfigHelper.GetConfigInt("MaxAttachmentSize")) { MessageBox.Show(this.Page, "上传的文件不能大于" + ConfigHelper.GetConfigInt("MaxAttachmentSize") + "KB!当前上传文件大小为:" + FileSize.ToString() + "KB"); return; } string extendname = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1).ToLowerInvariant(); string filename = fullfilename.Substring(0, fullfilename.LastIndexOf(".")); byte[] filedata; Stream filestream = FileUpload1.PostedFile.InputStream; #region 自动压缩上传的图片 if (ATMT_AttachmentBLL.IsImage(extendname)) { try { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(filestream); filestream.Position = 0; int width = originalImage.Width; if (width > 1024 || extendname == "bmp") { if (width > 1024) { width = 1024; } System.Drawing.Image thumbnailimage = ImageProcess.MakeThumbnail(originalImage, width, 0, "W"); MemoryStream thumbnailstream = new MemoryStream(); thumbnailimage.Save(thumbnailstream, System.Drawing.Imaging.ImageFormat.Jpeg); thumbnailstream.Position = 0; FileSize = (int)(thumbnailstream.Length / 1024); extendname = "jpg"; filestream = thumbnailstream; } } catch { } } #endregion filedata = new byte[filestream.Length]; filestream.Read(filedata, 0, (int)filestream.Length); filestream.Close(); EWF_Task_AttachmentBLL ta = new EWF_Task_AttachmentBLL(); ta.Model.Task = (int)ViewState["TaskID"]; ta.Model.Name = this.tbx_AttachmentName.Text.Trim(); if (ta.Model.Name == "") { ta.Model.Name = filename; } ta.Model.Description = this.tbx_AttachmentDescription.Text.Trim(); //ta.Model.FilePath = saveasfilename; ta.Model.UploadStaff = int.Parse(Session["UserID"].ToString()); ta.Model.FileSize = FileUpload1.PostedFile.ContentLength / 1024; ta.Model.FileType = extendname.ToUpper(); if (ta.Add(filedata) > 0) { BindAttachmentList(); } else { MessageBox.Show(this, "附件上传失败"); } } catch (Exception ex) { MessageBox.Show(this, ex.Message); return; } } else { MessageBox.Show(this, "未选择要上传的附件!"); return; } }
private void Download(Guid guid) { EWF_Task_AttachmentBLL bll = new EWF_Task_AttachmentBLL(guid); if (bll.Model == null) Response.End(); Response.Clear(); #region 确定ContentType switch (bll.Model.FileType.ToLower()) { case "jpg": case "jpeg": case "jpe": Response.ContentType = "image/jpeg"; break; case "gif": Response.ContentType = "image/gif"; break; case "bmp": Response.ContentType = "image/bmp"; break; case "png": Response.ContentType = "image/png"; break; case "tiff": case "tif": Response.ContentType = "image/tiff"; break; case "doc": case "docx": Response.ContentType = "application/ms-word"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "xls": case "xlsx": Response.ContentType = "application/ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "ppt": case "pptx": Response.ContentType = "application/ms-powerpoint"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "pdf": Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "zip": Response.ContentType = "application/zip"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "rar": Response.ContentType = "application/rar"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "txt": Response.ContentType = "text/plain"; break; case "htm": case "html": Response.ContentType = "text/html"; break; default: Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; } #endregion if (string.IsNullOrEmpty(bll.Model.FilePath)) { byte[] data = bll.GetData(); if (data != null) Response.OutputStream.Write(data, 0, data.Length); } else { string filepath = bll.Model.FilePath; if (filepath.StartsWith("~") || filepath.StartsWith("TaskAttachment")) filepath = Server.MapPath(filepath); if (!File.Exists(filepath)) { MessageBox.ShowAndRedirect(this, @"附件在服务器上不存在!", this.ResolveClientUrl("~/SubModule/desktop.aspx")); return; } Response.WriteFile(filepath); } Response.Flush(); Response.End(); }
//Upload Attachment protected void btn_Up_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { try { string fullfilename = FileUpload1.FileName; if (fullfilename.LastIndexOf(".") < 0) { MessageBox.Show(this, "无法识别的附件格式!"); return; } int FileSize = (FileUpload1.PostedFile.ContentLength / 1024); if (FileSize > ConfigHelper.GetConfigInt("MaxAttachmentSize")) { MessageBox.Show(this.Page, "上传的文件不能大于" + ConfigHelper.GetConfigInt("MaxAttachmentSize") + "KB!当前上传文件大小为:" + FileSize.ToString() + "KB"); return; } string extendname = fullfilename.Substring(fullfilename.LastIndexOf(".") + 1).ToLowerInvariant(); string filename = fullfilename.Substring(0, fullfilename.LastIndexOf(".")); byte[] filedata; Stream filestream = FileUpload1.PostedFile.InputStream; #region 自动压缩上传的图片 if (ATMT_AttachmentBLL.IsImage(extendname)) { try { System.Drawing.Image originalImage = System.Drawing.Image.FromStream(filestream); filestream.Position = 0; int width = originalImage.Width; if (width > 1024 || extendname == "bmp") { if (width > 1024) width = 1024; System.Drawing.Image thumbnailimage = ImageProcess.MakeThumbnail(originalImage, width, 0, "W"); MemoryStream thumbnailstream = new MemoryStream(); thumbnailimage.Save(thumbnailstream, System.Drawing.Imaging.ImageFormat.Jpeg); thumbnailstream.Position = 0; FileSize = (int)(thumbnailstream.Length / 1024); extendname = "jpg"; filestream = thumbnailstream; } } catch { } } #endregion filedata = new byte[filestream.Length]; filestream.Read(filedata, 0, (int)filestream.Length); filestream.Close(); EWF_Task_AttachmentBLL ta = new EWF_Task_AttachmentBLL(); ta.Model.Task = (int)ViewState["TaskID"]; ta.Model.Name = this.tbx_AttachmentName.Text.Trim(); if (ta.Model.Name == "") ta.Model.Name = filename; ta.Model.Description = this.tbx_AttachmentDescription.Text.Trim(); //ta.Model.FilePath = saveasfilename; ta.Model.UploadStaff = int.Parse(Session["UserID"].ToString()); ta.Model.FileSize = FileUpload1.PostedFile.ContentLength / 1024; ta.Model.FileType = extendname.ToUpper(); if (ta.Add(filedata) > 0) { BindAttachmentList(); } else { MessageBox.Show(this, "附件上传失败"); } } catch (Exception ex) { MessageBox.Show(this, ex.Message); return; } } else { MessageBox.Show(this, "未选择要上传的附件!"); return; } }
protected void gv_List_Attachment_RowDeleting(object sender, GridViewDeleteEventArgs e) { int id = (int)gv_List_Attachment.DataKeys[e.RowIndex]["ID"]; EWF_Task_AttachmentBLL att = new EWF_Task_AttachmentBLL(id); if (att.Model.UploadStaff == (int)Session["UserID"]) { att.Delete(); BindAttachmentList(); } else { MessageBox.Show(this, "对不起,您只能删除您自己上传的附件!"); } }
private void Download(Guid guid) { EWF_Task_AttachmentBLL bll = new EWF_Task_AttachmentBLL(guid); if (bll.Model == null) { Response.End(); } Response.Clear(); #region 确定ContentType switch (bll.Model.FileType.ToLower()) { case "jpg": case "jpeg": case "jpe": Response.ContentType = "image/jpeg"; break; case "gif": Response.ContentType = "image/gif"; break; case "bmp": Response.ContentType = "image/bmp"; break; case "png": Response.ContentType = "image/png"; break; case "tiff": case "tif": Response.ContentType = "image/tiff"; break; case "doc": case "docx": Response.ContentType = "application/ms-word"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "xls": case "xlsx": Response.ContentType = "application/ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "ppt": case "pptx": Response.ContentType = "application/ms-powerpoint"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "pdf": Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "zip": Response.ContentType = "application/zip"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "rar": Response.ContentType = "application/rar"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; case "txt": Response.ContentType = "text/plain"; break; case "htm": case "html": Response.ContentType = "text/html"; break; default: Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(bll.Model.Name + "." + bll.Model.FileType.ToLower())); break; } #endregion if (string.IsNullOrEmpty(bll.Model.FilePath)) { byte[] data = bll.GetData(); if (data != null) { Response.OutputStream.Write(data, 0, data.Length); } } else { string filepath = bll.Model.FilePath; if (filepath.StartsWith("~") || filepath.StartsWith("TaskAttachment")) { filepath = Server.MapPath(filepath); } if (!File.Exists(filepath)) { MessageBox.ShowAndRedirect(this, @"附件在服务器上不存在!", this.ResolveClientUrl("~/SubModule/desktop.aspx")); return; } Response.WriteFile(filepath); } Response.Flush(); Response.End(); }