上传文件到数据库
?
直接上代碼:
前臺:托一個asp.net上傳控件和一個button按鈕
?
后臺代碼:
protected void StartUpLoadFile(object sender, EventArgs e)
{
if (this.Fu_File.HasFile)
{
long fileLength = Fu_File.PostedFile.ContentLength;
byte[] bytes = new byte[fileLength];
Stream stream = Fu_File.PostedFile.InputStream;
stream.Read(bytes, 0, bytes.Length);
//Response.Write(bytes.Length + bytes.ToString());
//return;
InsertFileTable(bytes);
}
else
{
Response.Write("請先選擇一個文件!");
}
}
private void InsertFileTable(byte[] bytes)
{
using (SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=DbExam10;"))
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO FileStreamTable (FileBytes) VALUES (@fileBytes)", con);
cmd.Parameters.Add("@fileBytes", SqlDbType.VarBinary).Value = bytes;
if (cmd.ExecuteNonQuery() != -1)
{
Response.Write("文件上傳成功!");
}
}
}
轉載于:https://www.cnblogs.com/zhzhjieke/p/3584485.html
總結
- 上一篇: Jbox帮助文档,默认的属性含义
- 下一篇: 【转】XMPP_3920_最靠谱的中文翻