上传文件以二进制的形式存储
文件上传这个我看来有两种上传方法:一、上传到服务器上把文件地址存入数据库中 二、直接把文件以字节数存储
第一种方式比较常见:可以使用文件流的形式把文件写入到服务器端。
今天主要说明第二种方法:
因为我做的是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 = "删除"; } }