贴一个连接:全部源码下载连接
http://blog.csdn.net/comaple/archive/2010/12/23/6095065.aspx
第一步:获取文件流字节数组
/// <summary>
/// 获取文件流的字节数组
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
private static byte[] GetBinaryBufferFromFile(string filePath, ref string fileName)
{
fileName = filePath.Substring(filePath.LastIndexOf("//") + 1, filePath.Length - filePath.LastIndexOf("//") - 1);
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fileStream);
long fileLength = fileStream.Length;
byte[] buffer = new byte[fileLength];
buffer = binaryReader.ReadBytes((Int32)fileLength);
binaryReader.Close();
fileStream.Close();
return buffer;
}
第二步:入库我这里用到的是异步调用入库函数这是入库的部分,完整的代码可到我的csdn下载已经上传
贴一个连接:全部源码下载连接
http://blog.csdn.net/comaple/archive/2010/12/23/6095065.aspx
/// <summary>
/// 异步调用执行方法
/// </summary>
public static void work()
{
//要入库的文件
string sourceFilePath = "D://文件//源文件//源文件.rar";
byte[] buffer = GetBinaryBufferFromFile(sourceFilePath, ref fileName);
if (_importAndExport.ImportBinerary(buffer, fileName))
Console.Write("入库成功!文件名为:{0}", fileName);
System.Threading.Thread.Sleep(3000);
buffer = _importAndExport.GetBineraryBuffer(ref fileName);
Console.Write("获取文件成功!文件名:{0}", fileName.Trim());
System.Threading.Thread.Sleep(3000);
Console.Write("开始创建文件!");
System.Threading.Thread.Sleep(3000);
if (CreateFileByBinaryAndName(buffer, "D://文件//上传后的文件//" + fileName))
Console.WriteLine("创建文件{0}成功!", fileName.Trim());
}
第三步:将二进制流从数据库读出并在硬盘上重新创建文件
/// <summary>
/// 在服务器上创建文件
/// </summary>
/// <param name="buffers">字节数组</param>
/// <param name="fileName">文件名</param>
/// <param name="filePath">路径</param>
/// <returns></returns>
private static bool CreateFileByBinaryAndName(byte[] buffers, string filePath)
{
if (System.IO.File.Exists(filePath))
System.IO.File.Delete(filePath);
System.IO.FileStream fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write);
System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(fileStream);
streamWriter.Write(buffers);
streamWriter.Flush();
streamWriter.Close();
fileStream.Close();
return true;
}
贴一个连接:全部源码下载连接
http://blog.csdn.net/comaple/archive/2010/12/23/6095065.aspx