日期:2014-05-18  浏览次数:20787 次

求一个文件上传方法,保存到数据库中
不是以附件的形势保存到服务器上,而是以二进制流或其他方式保存到数据库的某个字段里,求该方法,以及下载的时候将该流还原成文件!
这个我应该说明白了吧!
1、支持文件上传,保存到数据库中(不是存文件路径)
2、支持下载文件,将流还原成文件。

------解决方案--------------------
数据库的image字段本身就是存储流的,直接通过数据库操作就可以

百度 C# 数据库保存图片
------解决方案--------------------
http://blog.csdn.net/llftc/article/details/7099663
------解决方案--------------------
写数据库:
OracleDataAccess da = new OracleDataAccess();
da.InserAttathment(fuFile.FileName, fuFile.FileBytes, "P_ZCWBM", txtRequestId.Text);
 public void InserAttathment(string fileName, byte[] tempBuff, string packageName, string requestId)
{
OracleConnection conn = new OracleConnection();
conn.ConnectionString = OracleFactory.ConfigHandler.MyConnectionString();
conn.Open();
OracleTransaction tx;
tx = conn.BeginTransaction();
OracleCommand cmd = new OracleCommand();
cmd = conn.CreateCommand();
cmd.Transaction = tx;
cmd.Parameters.Clear();
cmd.CommandText = packageName + ".PRO_SAVEATTACHMENTS";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("P_LOGO", "0"));
cmd.Parameters.Add(new OracleParameter("P_REQUESTID", requestId));
cmd.Parameters.Add(new OracleParameter("P_SEQ", "0"));
cmd.Parameters.Add(new OracleParameter("P_FILENAME", fileName));
cmd.Parameters.Add(new OracleParameter("P_BLOB", OracleType.Blob)).Value = tempBuff;
OracleParameter P_SPBD_CURSOR = new OracleParameter("P_ATTACHMENTS_CURSOR", OracleType.Cursor);
P_SPBD_CURSOR.Direction = ParameterDirection.Output;
cmd.Parameters.Add(P_SPBD_CURSOR);
try
{
cmd.ExecuteNonQuery();
tx.Commit();
}
catch (Exception ex)
{
ex.Message.ToString();
tx.Rollback();
conn.Close();
}
}
从数据库读取
DataTable dtAttachs = ah.GetAttachmentByRequestIdAndSeq(requestId,seq);
if (dtAttachs != null && dtAttachs.Rows.Count > 0)
{
byte[] FileData = (byte[])(object)dtAttachs.Rows[0]["ATTACHMENT"];
string filename = dtAttachs.Rows[0]["FILENAME"].ToString().Trim();
string sFolder = ConfigurationManager.AppSettings["DownLoadFolder"];
FileStream fs;
int len;
//文件长度
len = FileData.GetLength(0);
// 创建TEMP目录
if (!System.IO.Directory.Exists(sFolder))
{
System.IO.Directory.CreateDirectory(sFolder);
}
// 创建文件
string attachName = sFolder + "\\" + filename;
if (File.Exists(attachName)) File.Delete(attachName);

fs = File.Create(attachName);

// 写入数据
fs.Write(FileData, 0, len);

fs.Flush();
// 关闭对象
fs.Close();

FileInfo fi = new System.IO.FileInfo(attachName);
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;

Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode