数据库存储 varbinary(MAX) 类型的 Filecontent
public byte[] Filecontent { get; set; }
string source = HostingEnvironment.MapPath("~/App_Data/MonthProducOperate");
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data/MonthProducOperate", file.fileName);
//如果路径不存在,创建路径
if (!Directory.Exists(source))
{
Directory.CreateDirectory(source); //创建一个文件路径 存储文档信息
}
if (File.Exists(filePath))
{
File.Delete(filePath);
}
//第一种------------------------------------------------------
Byte[] returnBytes = file.Filecontent;
//byte[] returnBytes = new byte[file.CodeName.Length/2];
//for (int i = 0; i < returnBytes.Length; i++)
//{
// returnBytes[i] = Convert.ToByte(file.CodeName.Substring(1*2,2),16);
//}
Stream myStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite);
//使用FileStream对象实例化BinaryWriter二进制写入流对象
BinaryWriter myWriter=new BinaryWriter(myStream);
//以二进制方式向创建的文件中写入内容
myWriter.Write(returnBytes);
//关闭当前二进制写入流
myWriter.Close();
//关闭当前文件流
myStream.Close();
//第二种---------------------------------------------------------------------
FileStream fs = null;
BinaryWriter bw = null;
try
{
byte[] content = file.Filecontent;
fs = new FileStream(source, FileMode.CreateNew);
bw = new BinaryWriter(fs);
bw.Write(content, 0, content.Length);
}
catch (Exception ex)
{
}
finally
{
if (bw != null)
{
bw.Close();
bw = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
}