1private void Button1_Click(object sender, System.EventArgs e)//上传
2 {
3 int intDocLen = File1.PostedFile.ContentLength;
4 byte[] Docbuffer = new byte[intDocLen];
5 Stream objStream;
6 objStream = File1.PostedFile.InputStream;
7 objStream.Read(Docbuffer, 0, intDocLen);
8 OracleConnection con = new OracleConnection("user id=nj;data source=njoraclelocal;password=124578;");
9 con.Open();
10 OracleCommand cmd = new OracleCommand("InSert Into AAAA(WORD) Values(:word)",con);
11 OracleParameter para = new OracleParameter(":word",OracleType.Blob);
12 para.Value = Docbuffer;
13 para.Size = Docbuffer.Length;
14 cmd.Parameters.Add(para);
15 cmd.ExecuteNonQuery();
16 con.Close();
17// objStream.Close();
18 }
19
20 private void Button2_Click(object sender, System.EventArgs e)//下载
21 {
22// OracleConnection con = new OracleConnection("user id=nj;data source=NJORACLELOCAL;password=124578;");
23// OracleCommand cmd = new OracleCommand("Select * From AAAA",con);
24// con.Open();
25// OracleDataReader dr = cmd.ExecuteReader();
26// dr.Read();
27// Byte[] words = (Byte[])dr["WORD"];
28// Response.OutputStream.Write(words,0,words.Length);
29
30
31 string queryString = "Select * From AAAA where ROWID = 'AAAHsVAAJAAAAuHAAB'";
32 OracleConnection conn = new OracleConnection("user id=nj;data source=NJORACLELOCAL;password=124578;");
33 OracleCommand cmd = new OracleCommand(queryString,conn);
34 conn.Open();
35 OracleDataReader dr = cmd.ExecuteReader();
36 if (dr.Read())
37 {
38 Response.Clear();
39 Response.AddHeader("Content-Type", "application/msword");
40// Response.AddHeader("content-type","text/html");
41 Response.BinaryWrite((byte[])dr["WORD"]);//WDSJ为该BLOB字段名
42 }
43 dr.Close();
44 conn.Close();
45
46 }
47
48
这个很简单啊,数据库字段设为image.在可户端上传的时候用流的方式读取,其他的操作跟别的类型的没多少区别.以下有一个例子:
public void UpPic()
{
HttpPostedFile upFile = File1.PostedFile;//HttpPostedFile对象,用来读取上传文档的属性
int fileLength = upFile.ContentLength;//记录文件的长度
byte[] fileByte = new byte[fileLength];//用file的长度来初始化一个字节数组存储临时的文件
FileStream fs = new System.IO.FileStream(upFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] wordfile= br.ReadBytes((int)fs.Length); //然后把wordfile当作普通的参数传过去就可以了,应该会吧!
}
怎样把word文件上传到sql server数据库中,并且能够下载,数据的字段设为Image类型,然后保存图片的方法,保存Word.
回复人: zhf2002(歼10A) ( ) 信誉:92
public void ImageStored(Object sender,EventArgs e)
{
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string imgtitle = TextBox1.Text;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata,0,imgdatalen);
//string connstr="server=(local);database=ImageStore;Trusted_Connection=yes";
SqlConnection connection = new SqlConnection(CWMis.FinanceApp.Base.strConnection);
//SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("INSERT INTO ImgStore(imgtitle,imgtype,imgdata)VALUES ( @imgtitle, @imgtype,@imgdata )", connection );
SqlParameter paramTitle = new SqlParameter("@imgtitle", SqlDbType.VarChar,50 );
paramTitle.Value = imgtitle;
command.Parameters.Add( paramTitle);
SqlParameter paramData = new SqlParameter( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData );
SqlParameter paramType = new SqlParameter( "@imgtype", SqlDbType.VarChar,50 );
paramType.Value = imgtype;
command.Parameters.Add( paramType );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
}
回复人: stoway(X.G.Z) ( ) 信誉:100
<!-- * * * * * * * * * * * * * * * * * * * * * * * * 天天向上 * * * * * * * * * * * * * * * * * * * * * * * * * * * * if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[File]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) * * drop table [dbo].[File] * GO * * * CREATE TABLE [dbo].[File] ( * * [ID] [int] IDENTITY (1, 1) NOT NULL , * [fileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL 4 ? * * [fileType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , * [fileSize] [float] NULL 4 ? * * [fileContext] [image] NULL * ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] * * GO * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * --> <%@ Page language="c#"%> <%@ Import Namespace="System.Data.SqlClient" %> <%@ Import Namespace="System.Web" %> <%@ Import Namespace="System.Web.UI" %> <%@ Import Namespace="System.Web.UI.WebControls" %> <%@ Import Namespace="System.Data" %> <script runat="server"> override protected void OnInit(EventArgs e) { this.btUp.Click += new System.EventHandler(this.btUp_Click); this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand); this.Load += new System.EventHandler(this.Page_Load); base.OnInit(e); } void Page_Load(object sender, System.EventArgs e) { this.btUp.Attributes.Add("onclick","loading.style.display='';document.body.enabled=true"); if(Request["downFile"]!=null)//下载文件 { string strFileID = Request["downFile"]; SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password="); SqlDataAdapter adapt = new SqlDataAdapter("select * from [file] where id="+strFileID,conn); DataSet ds = new DataSet(); adapt.Fill(ds); if(ds.Tables.Count!=0 && ds.Tables[0].Rows.Count!=0) { Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment; filename="+ds.Tables[0].Rows[0]["fileName"].ToString()); byte[] context = (Byte[])ds.Tables[0].Rows[0]["fileContext"]; Response.OutputStream.Write(context,0,context.Length); Response.End(); } else { Response.Write("<font color=red>没有找到要下载的文件</font>"); } } else //显示文件列表 { ListFile(); } } void ListFile() { SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password="); SqlDataAdapter adapt = new SqlDataAdapter("select * from [file]",conn); DataSet ds = new DataSet(); adapt.Fill(ds); this.DataGrid1.DataSource = ds; this.DataGrid1.DataBind(); } void btUp_Click(object sender, System.EventArgs e) { if(File.PostedFile!=null && File.PostedFile.ContentLength!=0) { int size = File.PostedFile.ContentLength; SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password="); SqlCommand cmd = new SqlCommand("insert into [file] (fileName,fileType,fileSize,fileContext) values(@fileName,@fileType,@fileSize,@fileContext)",conn); //文件名 SqlParameter param = new SqlParameter("@fileName",SqlDbType.VarChar,50); param.Value = File.PostedFile.FileName.Substring(File.PostedFile.FileName.LastIndexOf("\\")+1); cmd.Parameters.Add(param); //文件类型 param = new SqlParameter("@fileType",SqlDbType.VarChar,50); param.Value = File.PostedFile.ContentType; cmd.Parameters.Add(param); //文件大小 param = new SqlParameter("@fileSize",SqlDbType.Float,8); param.Value = size; cmd.Parameters.Add(param); //文件内容 byte[] context = new Byte[size]; param = new SqlParameter("@fileContext",SqlDbType.Image); File.PostedFile.InputStream.Read(context,0,size); param.Value = context; cmd.Parameters.Add(param); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); ListFile(); } else { Response.Write("<font color=red>上传文件为空</font>"); } } void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { SqlConnection conn = new SqlConnection("Data Source=localhost;database=test;user id=sa;password="); SqlCommand cmd = new SqlCommand("delete [file] where id = @id",conn); SqlParameter param = new SqlParameter("@id",SqlDbType.Int); param.Value = e.Item.Cells[0].Text; cmd.Parameters.Add(param); conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); ListFile(); } </script> <HTML> <HEAD> <title>通用上传下载--支持所有格式文件</title> </HEAD> <body> <form id="WebForm5" method="post" runat="server" enctype="multipart/form-data"> <input id="File" type="file" runat="server"> <asp:button id="btUp" runat="server" Text="上 传"></asp:button><asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <HeaderStyle BackColor="#669966"></HeaderStyle> <Columns> <asp:BoundColumn DataField="ID" HeaderText="ID"> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="fileName" HeaderText="文件名"> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="fileType" HeaderText="文件类型"> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> </asp:BoundColumn> <asp:BoundColumn DataField="fileSize" HeaderText="文件大小"> <HeaderStyle HorizontalAlign="Center"></HeaderStyle> </asp:BoundColumn> <asp:TemplateColumn> <ItemTemplate> <asp:HyperLink Text="下载" NavigateUrl='<%# String.Format("{0}?downFile={1}",Request.CurrentExecutionFilePath,DataBinder.Eval(Container.DataItem,"ID"))%>' Runat="server"> </asp:HyperLink> </ItemTemplate> </asp:TemplateColumn> <asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn> </Columns> </asp:datagrid></form> <div id="loading" style="display:none;border-left:1px solid #EEEEEE;border-top:1px solid #EEEEEE;border-right:1px solid #666666;border-bottom:1px solid #666666;background-color:#DDDDDD;width:300px;heigth:200px;text-align:center"> 正在进行操作,请稍候…… </div> </body> </HTML>
上传: //得到提交的文件 Stream fileDataStream = MyFile.PostedFile.InputStream; //得到文件大小 int fileLength = MyFile.PostedFile.ContentLength; //创建数组 byte[] fileData = new byte[fileLength]; //把文件流填充到数组 fileDataStream.Read(fileData,0,fileLength); //得到文件名字 string fileTitle = MyFileName.Value; //得到文件类型 string fileType = MyFile.PostedFile.ContentType; //构建数据库连接,SQL语句,创建参数 SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=mdcija;Database=TestUploadFile"); SqlCommand command = new SqlCommand ("INSERT INTO TestFiles (MyFileName,MyFile,FileType)" + "VALUES (@MyFileName,@MyFile,@FileType)", connection); SqlParameter paramTitle = new SqlParameter ("@MyFileName", SqlDbType.VarChar,35); paramTitle.Value = fileTitle; command.Parameters.Add(paramTitle); SqlParameter paramData = new SqlParameter ("@MyFile", SqlDbType.Image); paramData.Value = fileData; command.Parameters.Add(paramData); SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,25); paramType.Value = fileType; command.Parameters.Add(paramType); //打开连接,执行查询 connection.Open(); command.ExecuteNonQuery(); connection.Close(); Message.Text="你的文件已经成功上载"; MyFileName.Value = ""; 下载: string sql="SELECT * FROM TestFiles WHERE ID = '" + Request.QueryString["ID"] + "'"; SqlConnection connection = new SqlConnection("Server=.;uid=sa;pwd=mdcija;Database=TestUploadFile"); SqlCommand command = new SqlCommand(sql, connection); connection.Open(); SqlDataReader dr = command.ExecuteReader(); if(dr.Read()) { Response.Clear(); Response.AddHeader("Content-Type",dr["FileType"].ToString()); Response.BinaryWrite((byte[])dr["MyFile"]); } dr.Close(); connection.Close();
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[TestFiles]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[TestFiles] GO CREATE TABLE [dbo].[TestFiles] ( [id] [int] IDENTITY (1, 1) NOT NULL , [MyFileName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [FileType] [varchar] (50) COLLATE Chinese_PRC_CI_AS NOT NULL , [MyFile] [image] NOT NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO this is create table code!