上傳文件以二進制的形式存儲


文件上傳這個我看來有兩種上傳方法:一、上傳到服務器上把文件地址存入數據庫中 二、直接把文件以字節數存儲

第一種方式比較常見:可以使用文件流的形式把文件寫入到服務器端。

今天主要說明第二種方法:

因為我做的是web項目,所以上傳工具就用到了FileUpload控件如何實現的呢,不廢話上代碼

default.aspx

<head runat="server">
    <title></title>
    <script type="text/javascript">
        function UploadFile() {
            var frmUpload = ifu.document.getElementById("frmUpload");
            //在本地測試這種取值會報錯,好像是我本機的IE有問題導致的
            //var fuFileUpload = frmUpload.document.getElementById("fuFileUpload");
            //var btnFileUpload = frmUpload.document.getElementById("btnFileUpload");
            var fuFileUpload = frmUpload.fuFileUpload;
            var btnFileUpload = frmUpload.btnFileUpload;
            try {
                fuFileUpload.click();
            }
            catch (e) {
                return false;
            }

            if (fuFileUpload.value != "") {
                btnFileUpload.click();
            }
        }
        function ShowMsgAndRefresh() {
            document.getElementById('<%=btnHidView.ClientID%>').click();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <iframe id="ifu" src="AttachFileUpload.aspx" style="display: none;"></iframe>
    <asp:Button ID="btnHidView" runat="server" Style="display: none;" OnClick="btnHidView_Click" />

  <asp:LinkButton ID="lbtnAttachFiles" runat="server" OnClick="lbtnAttachFiles_Click"
                                                        Width="160px" CssClass="ShortNameShow"></asp:LinkButton>
                                        <div style="float: left; margin: 24px 10px 0px 10px;">
                            <asp:Button ID="btnRecordDelete" runat="server" Text="附件" OnClick="btnRecordDelete_Click" /></div>
    </form>
</body>

  這里把文件上傳單獨放到一個頁面,我們利用iframe來加載【附件 AttachFileUpload】的頁面

AttachFileUpload.aspx

    <form id="frmUpload" runat="server" style="height: auto;">
        <div >
            <asp:FileUpload ID="fuFileUpload" runat="server" Style="height: 0px;display: none;" />
            <asp:Button ID="btnFileUpload" runat="server" Text="Button" 
                Style="height: 0px;display: none;" onclick="btnFileUpload_Click" />
        </div>
    </form>

  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

/// <summary>
        /// 附件下載
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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();
        }
        /// <summary>
        /// 附件添加刪除處理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecordDelete_Click(object sender, EventArgs e)
        {
            if (btnRecordDelete.Text.Trim() == "附件")
            {
                ClientScript.RegisterStartupScript(this.Page.GetType(), "OpenFileDialogAndInsert", "<Script language=JavaScript>window.attachEvent('onload',UploadFile);</Script>");
            }
            else if (btnRecordDelete.Text.Trim() == "刪除")
            {
                Session["FileName"] = string.Empty;
                Session["FileData"] = null;

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

                btnRecordDelete.Text = "附件";
            }
        }

        /// <summary>
        /// 附件添加完成后的處理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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 = "刪除";
            }
        }

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM