当前位置:Gxl网 > 互联网 > 上传和下载附件功能

上传和下载附件功能

时间:2021-07-01 10:21:17 帮助过:2人阅读

HTML上传部分 
文件名:文件(80M以内):
                                
                                

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", "");
                        ShowControl();
                    }
                    else
                    {
                        Page.RegisterClientScriptBlock("TS", "");
                    }

                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            else
            {
                Page.RegisterClientScriptBlock("TS", "");
            }
        }

绑定显示上传成功的附件:

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 += "";
                }
            }
            else
            {
                html = "

暂无附件

"; } LAttHtml.Text = html; }

        /// 
        /// 获取下载URL
        /// 
        /// 
        /// 
        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
            {

            }

        }

 

上传和下载附件功能,布布扣,bubuko.com