存入数据库过程,采用asp:FileUpload控件进行文件上传:
int intDocLen = FileUpload1.PostedFile.ContentLength; byte[] Docbuffer = new byte[intDocLen]; Stream objStream; objStream = FileUpload1.PostedFile.InputStream; objStream.Read(Docbuffer, 0, intDocLen); string FileID = DateTime.Now.ToString("yyyyMMddHHmmssfff"); SqlConnection BooksConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()); SqlCommand cmdUploadDoc = new SqlCommand("FileSystem_UploadFile", BooksConn); cmdUploadDoc.CommandType = CommandType.StoredProcedure; cmdUploadDoc.Parameters.Add("@ID", SqlDbType.VarChar, 50); cmdUploadDoc.Parameters.Add("@FileName", SqlDbType.VarChar, 50); cmdUploadDoc.Parameters.Add("@FileImageName", SqlDbType.VarChar, 50); cmdUploadDoc.Parameters.Add("@FileType", SqlDbType.VarChar, 50); cmdUploadDoc.Parameters.Add("@FileState", SqlDbType.VarChar, 50); cmdUploadDoc.Parameters.Add("@FileDescription", SqlDbType.VarChar); cmdUploadDoc.Parameters.Add("@FileData", SqlDbType.Image); cmdUploadDoc.Parameters.Add("@UpdateTime", SqlDbType.DateTime); cmdUploadDoc.Parameters.Add("@UpdateUser", SqlDbType.VarChar, 50); cmdUploadDoc.Parameters.Add("@Mark", SqlDbType.VarChar); cmdUploadDoc.Parameters.Add("@BindID", SqlDbType.VarChar, 50); cmdUploadDoc.Parameters["@ID"].Value = FileID; cmdUploadDoc.Parameters["@FileName"].Value = FileName.Text; cmdUploadDoc.Parameters["@FileImageName"].Value = FileUpload1.FileName; cmdUploadDoc.Parameters["@FileType"].Value = FileType.SelectedValue.ToString(); cmdUploadDoc.Parameters["@FileState"].Value = "新建"; cmdUploadDoc.Parameters["@FileDescription"].Value = Description.Text; cmdUploadDoc.Parameters["@FileData"].Value = Docbuffer; cmdUploadDoc.Parameters["@UpdateTime"].Value = DateTime.Now; cmdUploadDoc.Parameters["@UpdateUser"].Value = Context.User.Identity.Name; cmdUploadDoc.Parameters["@Mark"].Value = Remark.Text; cmdUploadDoc.Parameters["@BindID"].Value = BindID.Text; BooksConn.Open(); cmdUploadDoc.ExecuteNonQuery(); BooksConn.Close(); ErrorMessage.Text = "添加文件成功!";
上传文件需要配合数据库存储过程,建立存储过程SQL代码如下:
CREATE PROCEDURE FileSystem_UploadFile @ID varchar(50), @FileName varchar(50), @FileImageName varchar(50), @FileType varchar(50), @FileState varchar(50), @FileDescription varchar(max), @FileData image, @UpdateTime Datetime, @UpdateUser varchar(50), @Mark varchar(max), @BindID varchar(50) AS INSERT FileSystem(ID,FileName,FileImageName,FileType,FileState,FileDescription,FileData,UpdateTime,UpdateUser,Mark,BindID) VALUES (@ID,@FileName,@FileImageName,@FileType,@FileState,@FileDescription,@FileData,@UpdateTime,@UpdateUser,@Mark,@BindID) GO
文件取出并下载过程:
byte[] Docbuffer; string strsql = "SELECT * FROM FileSystem WHERE (ID = '" + Request["FileID"].ToString() + "')"; SqlConnection BooksConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()); SqlCommand cmdDownloadDoc = new SqlCommand(strsql, BooksConn); cmdDownloadDoc.Connection.Open(); using (SqlDataReader sdr = cmdDownloadDoc.ExecuteReader()) { if (sdr.Read()) { String strFileName = sdr["FileImageName"].ToString(); String savePath = Server.MapPath("./Temp/" + strFileName); //服务器路径转换为物理路径 Docbuffer = (byte[])sdr["FileData"]; using (FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write)) { fs.Write(Docbuffer, 0, Docbuffer.Length); } //判断文件类型并做下载处理 string[] stmp = strFileName.Split('.'); string FilePost = stmp[stmp.Length - 1];//取扩展名 switch (FilePost) { case "pdf"://PDF文件 Response.ContentType = "application/PDF"; break; case "doc": Response.ContentType = "application/msword"; break; case "xls": Response.ContentType = "application/vnd.ms-excel"; break; case "jpg": Response.ContentType = "image/jpeg"; break; case "gif": Response.ContentType = "image/gif"; break; case "png": Response.ContentType = "image/png"; break; case "txt": Response.ContentType = "text/plain"; break; default: Response.ContentType = "application/octet-stream"; break; } Response.AddHeader("Content-Disposition", "filename=" + strFileName);//指定文件名 Response.WriteFile(savePath);//写入客户端 Response.Flush();//客户更新 Response.Close();//写入关闭 //Session.Remove("Report");//移除Report Session } else { Response.Write("<script>window.alert('文件不存在!')</script>"); Response.End(); } } cmdDownloadDoc.Connection.Close();