有關C#上傳文件到MSSQL數據存儲問題 有關C#上傳文件到MSSQL數據存儲問題各位,用C#代碼怎樣寫把用戶端指定的文件(C/S結構)保存到數據庫Binary字段中,反過來,又怎樣從數據庫下載到用戶端,形成對應的文件。注:如果文件較大,程式要告訴用戶上傳或下載文件的進度。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 上传StringBuilder strbSqlFile = new StringBuilder();if (FileUpload1.HasFile) { SqlCommand cmd = new SqlCommand(); cmd.Connection = db.conn; cmd.CommandTimeout = 300; strbSqlFile.Append("INSERT INTO TableName VALUES ("); HttpPostedFile upFile = FileUpload1.PostedFile; int upFileLength = upFile.ContentLength; byte[] FileArray = new Byte[upFileLength]; Stream UpFileStream = upFile.InputStream; UpFileStream.Read(FileArray, 0, upFileLength); strbSqlFile.Append(" File=@File) "); cmd.Parameters.Add("@File", SqlDbType.VarBinary); cmd.Parameters["@File"].Value = FileArray; strbSqlFile.Append("WHERE PTNO='" + strPTNO + "' AND Seq='" + (maxSeq + 1) cmd.CommandText = strbSqlFile.ToString(); cmd.ExecuteNonQuery(); }下载获取的数据放在dataset中byte[] byteData = (byte[])ds.Tables[0].Rows[0][0]; Response.Clear(); Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift-JIS"); Response.ContentType = "application/msword; charset=Shift-JIS"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlPathEncode(strFileName) ); Response.AddHeader("Content-Length ", byteData.Length.ToString()); Response.Flush(); Response.BinaryWrite(byteData); Response.End(); } 很简单的,参考:http://topic.csdn.net/t/20051228/15/4486561.html 您提示的這種方法是可以上傳和下載文件數據庫,但我怎樣告訴用戶已經完成了多少呢?(注:我是用C#寫C/S結構的程式) http://www.cnblogs.com/gudieaofei/articles/1265372.html 誰能告訴我WinFrom中怎樣獲取客戶端保存文件到MS SQL數據庫中的方法,並能告知用戶保存的進度...... 我是用C# Winform寫上傳文件到數據庫中,謝謝指點 一张图片,对它进行拖动 WebRequest 如何实现中文地址不编码 急求 测量 ZedGraph 两坐标间的距离 C#中如何开发Web中使用的Excel控件(十万火急,在线等) 对菜鸟来说超难的问题,以ASCII码显示二进制文件。 C# FTP问题 刚学编程2个月的猛人提出变态惊人问题!! login控件怎样取username publish一个程序以后名字如何更改 请教关于ado.net的问题 向dataGridView单元格中输入一个值,怎么马上保存上这个值。 求一个SQL语句,前辈们来帮帮忙了~~~
if (FileUpload1.HasFile)
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = db.conn;
cmd.CommandTimeout = 300; strbSqlFile.Append("INSERT INTO TableName VALUES ("); HttpPostedFile upFile = FileUpload1.PostedFile;
int upFileLength = upFile.ContentLength;
byte[] FileArray = new Byte[upFileLength];
Stream UpFileStream = upFile.InputStream;
UpFileStream.Read(FileArray, 0, upFileLength);
strbSqlFile.Append(" File=@File) ");
cmd.Parameters.Add("@File", SqlDbType.VarBinary);
cmd.Parameters["@File"].Value = FileArray; strbSqlFile.Append("WHERE PTNO='" + strPTNO + "' AND Seq='" + (maxSeq + 1)
cmd.CommandText = strbSqlFile.ToString();
cmd.ExecuteNonQuery();
}
下载获取的数据放在dataset中
byte[] byteData = (byte[])ds.Tables[0].Rows[0][0]; Response.Clear();
Response.ContentEncoding = System.Text.Encoding.GetEncoding("Shift-JIS");
Response.ContentType = "application/msword; charset=Shift-JIS";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlPathEncode(strFileName) );
Response.AddHeader("Content-Length ", byteData.Length.ToString());
Response.Flush();
Response.BinaryWrite(byteData); Response.End();
}
http://topic.csdn.net/t/20051228/15/4486561.html
(注:我是用C#寫C/S結構的程式)