将文件上传、下载(以二进制流保存到数据库)
2013-05-21 12:53
302 查看
1、将文件以二进制流的格式写入数据库
首先获得文件路径,然后将文件以二进制读出保存在一个二进制数组中,与数据库建立连接,在SQL语句中将二进制数组赋值给相应的参数,完成向数据库中写入文件的操作
[csharp] view
plaincopy
/// 将文件流写入数据库
/// </summary>
/// <param name="filePath">存入数据库文件的路径</param>
/// <param name="id">数据库中插入文件的行标示符ID</param>
/// <returns></returns>
public int UploadFile(string filePath, string id)
{
byte[] buffer = null;
int result = 0;
if (!string.IsNullOrEmpty(fil
e7f4
ePath))
{
String file = HttpContext.Current.Server.MapPath(filePath);
buffer = File.ReadAllBytes(file);
using (SqlConnection conn = new SqlConnection(DBOperator.ConnString))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "update DomesticCompanyManage_Main_T set ZBDocumentFile = @fileContents where MainID ='" + id + "'";;
cmd.Parameters.AddRange(new[]{
new SqlParameter("@fileContents",buffer)
});
conn.Open();
result = cmd.ExecuteNonQuery();
conn.Close();
}
}
return result;
}
else
return 0;
}
2、从数据库中将文件读出并建立相应格式的文件
从数据库中读取文件,只需根据所需的路径建立相应的文件,然后将数据库中存放的二进制流写入新建的文件就可以了
如果该目录下有同名文件,则会将原文件覆盖掉
[csharp] view
plaincopy
//从数据库中读取文件流
//shipmain.Rows[0]["ZBDocument"],文件的完整路径
//shipmain.Rows[0]["ZBDocumentFile"],数据库中存放的文件流
if (shipmain.Rows[0]["ZBDocumentFile"] != DBNull.Value)
{
int arraySize = ((byte[])shipmain.Rows[0]["ZBDocumentFile"]).GetUpperBound(0);
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(shipmain.Rows[0]["ZBDocument"].ToString()), FileMode.OpenOrCreate, FileAccess.Write);//由数据库中的数据形成文件
fs.Write((byte[])shipmain.Rows[0]["ZBDocumentFile"], 0, arraySize);
fs.Close();
}
首先获得文件路径,然后将文件以二进制读出保存在一个二进制数组中,与数据库建立连接,在SQL语句中将二进制数组赋值给相应的参数,完成向数据库中写入文件的操作
[csharp] view
plaincopy
/// 将文件流写入数据库
/// </summary>
/// <param name="filePath">存入数据库文件的路径</param>
/// <param name="id">数据库中插入文件的行标示符ID</param>
/// <returns></returns>
public int UploadFile(string filePath, string id)
{
byte[] buffer = null;
int result = 0;
if (!string.IsNullOrEmpty(fil
e7f4
ePath))
{
String file = HttpContext.Current.Server.MapPath(filePath);
buffer = File.ReadAllBytes(file);
using (SqlConnection conn = new SqlConnection(DBOperator.ConnString))
{
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "update DomesticCompanyManage_Main_T set ZBDocumentFile = @fileContents where MainID ='" + id + "'";;
cmd.Parameters.AddRange(new[]{
new SqlParameter("@fileContents",buffer)
});
conn.Open();
result = cmd.ExecuteNonQuery();
conn.Close();
}
}
return result;
}
else
return 0;
}
2、从数据库中将文件读出并建立相应格式的文件
从数据库中读取文件,只需根据所需的路径建立相应的文件,然后将数据库中存放的二进制流写入新建的文件就可以了
如果该目录下有同名文件,则会将原文件覆盖掉
[csharp] view
plaincopy
//从数据库中读取文件流
//shipmain.Rows[0]["ZBDocument"],文件的完整路径
//shipmain.Rows[0]["ZBDocumentFile"],数据库中存放的文件流
if (shipmain.Rows[0]["ZBDocumentFile"] != DBNull.Value)
{
int arraySize = ((byte[])shipmain.Rows[0]["ZBDocumentFile"]).GetUpperBound(0);
FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(shipmain.Rows[0]["ZBDocument"].ToString()), FileMode.OpenOrCreate, FileAccess.Write);//由数据库中的数据形成文件
fs.Write((byte[])shipmain.Rows[0]["ZBDocumentFile"], 0, arraySize);
fs.Close();
}
相关文章推荐
- 将文件上传、下载(以二进制流保存到数据库)实现代码
- C#将文件上传、下载(以二进制流保存到数据库)
- 将文件上传、下载(以二进制流保存到数据库)
- 将文件上传、下载(以二进制流保存到数据库)实现代码
- 文件以二进制流保存到数据库及下载
- 文件以二进制流保存到数据库及下载(转载)
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- WPF用流的方式上传/显示/下载图片文件(保存在数据库)
- springMvc 中 Excel批量数据上传,利用POI解析数据保存入数据库 及模板文件下载
- ASP.NET MVC文件上传下载Demo(文件保存在数据库)
- 上传下载保存到数据库的image字段中的文件
- 上传文件至数据库并下载
- springmvc和servlet在上传和下载文件(保持文件夹和存储数据库Blob两种方式)
- 大文件上传代码,根据下拉列表框选中的市县镇名保存文件到指定目录下,并将文件相关信息写进数据库
- Java使用servlet实现文件上传至数据库和从数据库下载文件
- 文件上传与下载之数据库实现
- thinkphp表单上传文件并将文件路径保存到数据库中
- 如何直接下载保存在Oracl数据库中Blob字段的文件(不生成临时文件)???
- PHP 合理配置实现文件上传及保存文件到数据库
- 文件上传表单 上传文件的细节 文件上传下载和数据库结合