上传文件以二进制的形式存储

文件上传这个我看来有两种上传方法:一、上传到服务器上把文件地址存入数据库中 二、直接把文件以字节数存储

第一种方式比较常见:可以使用文件流的形式把文件写入到服务器端。

今天主要说明第二种方法:

因为我做的是web项目,所以上传工具就用到了FileUpload控件如何实现的呢,不废话上代码

default.aspx


    
    


    

  这里把文件上传单独放到一个页面,我们利用iframe来加载【附件 AttachFileUpload】的页面

AttachFileUpload.aspx

    

  AttachFileUpload.aspx.cs

protected void btnFileUpload_Click(object sender, EventArgs e)
        {
            string l_strFullName = fuFileUpload.PostedFile.FileName;
            string l_strFileName = l_strFullName.Substring(l_strFullName.LastIndexOf("\\") + 1);
            try
            {
                byte[] l_bytFile = fuFileUpload.f.FileBytes;
                Session["FileName"] = l_strFileName;
                Session["FileData"] = l_bytFile;
                ClientScript.RegisterStartupScript(this.GetType(), "ShowMsgAndRefresh", "parent.ShowMsgAndRefresh()", true);
            }
            catch
            {
                return;
            }
        }

Default.aspx.cs

/// 
        /// 附件下载
        /// 
        /// 
        /// 
        protected void lbtnAttachFiles_Click(object sender, EventArgs e)
        {
            string l_strFileName = Session["FileName"].ToString();
            byte[] l_bytFileData = Session["FileData"] as byte[];

            HttpContext.Current.Response.Clear();
            l_strFileName = System.Web.HttpUtility.UrlEncode(l_strFileName);
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + l_strFileName);
            if (l_bytFileData.Length == 0)
            {
                l_bytFileData = System.Text.Encoding.Unicode.GetBytes(" ");
            }
            HttpContext.Current.Response.BinaryWrite(l_bytFileData);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
        /// 
        /// 附件添加删除处理
        /// 
        /// 
        /// 
        protected void btnRecordDelete_Click(object sender, EventArgs e)
        {
            if (btnRecordDelete.Text.Trim() == "附件")
            {
                ClientScript.RegisterStartupScript(this.Page.GetType(), "OpenFileDialogAndInsert", "");
            }
            else if (btnRecordDelete.Text.Trim() == "删除")
            {
                Session["FileName"] = string.Empty;
                Session["FileData"] = null;

                lbtnAttachFiles.Text = Session["FileName"].ToString();
                //lbtnAttachFiles.ToolTip = Session["FileName"].ToString();

                btnRecordDelete.Text = "附件";
            }
        }

        /// 
        /// 附件添加完成后的处理
        /// 
        /// 
        /// 
        protected void btnHidView_Click(object sender, EventArgs e)
        {
            lbtnAttachFiles.Text = Session["FileName"].ToString();
            //lbtnAttachFiles.ToolTip = Session["FileName"].ToString();

            if (!string.IsNullOrEmpty(lbtnAttachFiles.Text.Trim()))
            {
                btnRecordDelete.Text = "删除";
            }
        }

  

 

转载于:https://www.cnblogs.com/WarBlog/p/5646154.html

你可能感兴趣的:(上传文件以二进制的形式存储)