扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
CREATE TABLE tblBooksUpload ( DocID int NOT NULL IDENTITY Primary Key , DocTitle varchar (200) , Doc image, DocType varchar (50) , Entrydate datetime Default GetDate() ) |
CREATE PROCEDURE uSP_BooksUploadFile @Title varchar(200), @Doc image, @DocType varchar(4) AS INSERT tblBooksUpload(DocTitle,Doc,DocType) VALUES (@Title,@Doc,@DocType) GO |
<form id="frmUpload" method="post" enctype="multipart/form-data" runat="server"> <span>Title</span><br> <asp:textbox id="txtTitle" runat="server" EnableViewState="False"></asp:textbox> <asp:requiredfieldvalidator id="valrTitle" runat="server" ErrorMessage="* Required" ControlToValidate="txtTitle">* Required</asp:requiredfieldvalidator> <br> <br> <span>Docutment to Upload</span><br> <input id="txtFileContents" type="file" runat="server" NAME="txtFileContents"> <br> <br> <asp:button id="btnSubmit" Text="Submit" Runat="server"></asp:button> </form> |
int intDocLen = txtFileContents.PostedFile.ContentLength; |
byte[] Docbuffer = new byte[intDoclen]; |
Stream objStream; objStream = txtFileContents.PostedFile.InputStream; objStream.Read(Docbuffer,0,intDocLen); |
在以上代码中,读取缓存的时候,从缓存的0位置开始,直到整个文件的长度,其实,这就是整个文件或者整个缓存的大小。
第三步
现在我们需要做的就是将缓存数据保存到数据库,我们已经直到数据表结构,这样,我们通过编写简单的SQL语句就可以实现这个功能。在上面的内容中,我们编写了一个存储过程,在程序中,我们只要建立SqlCommand对象并且将这个存储过程传递给它,并设置“@Doc”参数取得缓存数据就可以了:
cmdUploadDoc = new SqlCommand("uSP_BooksUploadFile",BooksConn); cmdUploadDoc.CommandType = CommandType.StoredProcedure; cmdUploadDoc.Parameters.Add("@Title ",SqlDbType.VarChar,200); cmdUploadDoc.Parameters.Add("@Doc",SqlDbType.Image); cmdUploadDoc.Parameters.Add("@DocType",SqlDbType.VarChar,4); cmdUploadDoc.Parameters[0].Value = txtTitle.Text; cmdUploadDoc.Parameters[1].Value = Docbuffer; cmdUploadDoc.Parameters[2].Value = strDocType; |
private void btnSubmit_Click(object sender, System.EventArgs e) { string strDocExt; //strDocType用于保存上传文件的类型 string strDocType; //用于保存文件大小 int intDocLen; //Stream用于读取上传数据 Stream objStream; SqlConnection BooksConn; SqlCommand cmdUploadDoc; if(IsValid) { if(txtFileContents.PostedFile != null) { //文件类型 strDocExt = CString.Right(txtFileContents.PostedFile.FileName,4).ToLower(); switch(strDocExt) { case ".doc": strDocType = "doc"; break; case ".ppt": strDocType = "ppt"; break; case ".htm": strDocType = "htm"; break; case ".html": strDocType = "htm"; break; case ".jpg": strDocType = "jpg"; break; case ".gif": strDocType = "gif"; break; default: strDocType = "txt"; break; } //上传文件具体内容 intDocLen = txtFileContents.PostedFile.ContentLength; byte[] Docbuffer = new byte[intDocLen]; objStream = txtFileContents.PostedFile.InputStream; //文件保存到缓存 //缓存将保存到数据库 objStream.Read(Docbuffer ,0,intDocLen); BooksConn = new SqlConnection("Server=Server;UID=sa;Database=Books"); cmdUploadDoc = new SqlCommand("uSP_BooksUploadFile",BooksConn); cmdUploadDoc.CommandType = CommandType.StoredProcedure; cmdUploadDoc.Parameters.Add("@Title ",SqlDbType.VarChar,200); cmdUploadDoc.Parameters.Add("@Doc",SqlDbType.Image); cmdUploadDoc.Parameters.Add("@DocType",SqlDbType.VarChar,4); cmdUploadDoc.Parameters[0].Value = txtTitle.Text; cmdUploadDoc.Parameters[1].Value = Docbuffer ; cmdUploadDoc.Parameters[2].Value = strDocType; BooksConn.Open(); cmdUploadDoc.ExecuteNonQuery(); BooksConn.Close(); } } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者