HTML上传部分 文件名:<asp:TextBox ID="tbFileName" runat="server" Width="150px"></asp:TextBox>文件(80M以内): <asp:FileUpload ID="FileUpload2" runat="server" /> <asp:Button ID="Button1" runat="server" CssClass="buttoncss" Text="上传" OnClick="Button1_Click" /> protected void Button1_Click(object sender, EventArgs e) { if (FileUpload2.HasFile && ViewState["pid"] != null) { string path = Server.MapPath("ATT"); //创建目录 string pid = ViewState["pid"].ToString(); if (!Directory.Exists(path + "/" + pid)) { Directory.CreateDirectory(path + "/" + pid); } path += "/" + pid; string filename = ""; if (!string.IsNullOrEmpty(tbFileName.Text)) { string hz = FileUpload2.FileName.Substring(FileUpload2.FileName.IndexOf(‘.‘), FileUpload2.FileName.Length - FileUpload2.FileName.IndexOf(‘.‘)); filename = tbFileName.Text.Trim() + hz; } else { string Extension = System.IO.Path.GetExtension(FileUpload2.FileName).ToLower(); string FileName = FileUpload2.FileName.Substring(0, FileUpload2.FileName.IndexOf(Extension)); FileName += DateTime.Now.ToString("yyyyMMddhhmmss"); filename = FileName + Extension; } try { FileUpload2.SaveAs(path + "/" + filename); //保存信息 AttachmentModel uam = new AttachmentModel(); uam.AttFileName = filename.Substring(0, filename.IndexOf(‘.‘)); uam.ProjectId = Convert.ToInt32(pid); uam.AttPath = "ATT/" + pid + "/" + filename; LoginUserInfoModel lum = new LoginUserInfoModel(); lum = (LoginUserInfoModel)Session["LoginUserInfoModel"]; uam.RecordAccountNoID = lum.LoginUserID; if (IPB.SaveATT(uam)) { Page.RegisterClientScriptBlock("TS", "<script>jError(‘上传成功‘, { TimeShown: 1000, VerticalPosition: ‘center‘ });;</script>"); ShowControl(); } else { Page.RegisterClientScriptBlock("TS", "<script>jError(‘上传失败‘, { TimeShown: 1000, VerticalPosition: ‘center‘ });;</script>"); } } catch (Exception ex) { throw ex; } } else { Page.RegisterClientScriptBlock("TS", "<script>jError(‘请选择文件‘, { TimeShown: 1000, VerticalPosition: ‘center‘ });;</script>"); } }
绑定显示上传成功的附件:
private void BindATT(string pid) { DataTable dt = IPB.GetATT(pid); string html = ""; if (dt != null && dt.Rows != null && dt.Rows.Count > 0) { foreach (DataRow dr in dt.Rows) { html += "<div style=‘float:left;margin-top:10px;clear:both;‘> <a href=‘" + GetLink(dr["AttPath"].ToString()) + "‘ target=‘_blank‘ style=‘color:Blue;‘>" + dr["AttFileName"].ToString() + " </a>"; html += "<a href=\"javascript:void(0);\" onclick=\"DelAtt(‘" + dr["id"].ToString() + "‘,‘" + HelpBLL.Encode(pid) + "‘);\" style=‘ font-size:12px;‘>删除</a><br/>"; html += "</div>"; } } else { html = "<p style=‘color:red;‘>暂无附件</p>"; } LAttHtml.Text = html; }
/// <summary> /// 获取下载URL /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetLink(string url) { string str = "Download.aspx?FileName=" + HttpUtility.UrlEncode(url) + ""; return str; }
Download.aspx 只有后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ResponseFile(HttpUtility.UrlDecode(Request.QueryString["FileName"])); } } public void ResponseFile(string fileName) { try { String FullFileName = HttpContext.Current.Server.MapPath(fileName); FileInfo DownloadFile = new FileInfo(FullFileName); Response.Clear(); Response.Expires = 0; Response.Buffer = true; Response.Charset = "utf-8"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); Response.TransmitFile(DownloadFile.FullName); Response.End(); } catch { } }
时间: 2024-10-24 04:52:41