145 lines
5.0 KiB
C#
145 lines
5.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Collections;
|
|
using System.IO;
|
|
using System.Web;
|
|
using System.Configuration;
|
|
|
|
namespace BLL
|
|
{
|
|
public static class FileStructService
|
|
{
|
|
/// <summary>
|
|
/// 获取附件数据流类
|
|
/// </summary>
|
|
/// <param name="attachUrl">附件路径</param>
|
|
/// <returns></returns>
|
|
public static List<byte[]> GetFileStructByAttachUrl(string attachUrl)
|
|
{
|
|
List<byte[]> fileContext = new List<byte[]>();
|
|
if (!String.IsNullOrEmpty(attachUrl))
|
|
{
|
|
string filePath = string.Empty;
|
|
string physicalpath = Funs.RootPath;
|
|
//HttpContext.Current.Request.PhysicalApplicationPath;
|
|
filePath = physicalpath + attachUrl;
|
|
if (File.Exists(filePath))
|
|
{
|
|
FileInfo fileInfo = new FileInfo(filePath);
|
|
Stream stream = fileInfo.OpenRead();
|
|
//读取指定大小的文件流内容到uploadFile.Context以便上传
|
|
int b;
|
|
while (stream.Position > -1 && stream.Position < stream.Length)
|
|
{
|
|
if (stream.Length - stream.Position >= 20000000)
|
|
{
|
|
b = 20000000;
|
|
}
|
|
else
|
|
{
|
|
b = (int)(stream.Length - stream.Position);
|
|
}
|
|
|
|
byte[] filebyte = new byte[b];
|
|
stream.Read(filebyte, 0, b);
|
|
fileContext.Add(filebyte);
|
|
}
|
|
stream.Close();
|
|
}
|
|
}
|
|
|
|
return fileContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取附件数据流类 多附件的情况
|
|
/// </summary>
|
|
/// <param name="attachUrl">附件路径</param>
|
|
/// <returns></returns>
|
|
public static List<byte[]> GetMoreFileStructByAttachUrl(string attachUrl)
|
|
{
|
|
List<byte[]> fileContext = new List<byte[]>();
|
|
if (!String.IsNullOrEmpty(attachUrl))
|
|
{
|
|
string[] strs = attachUrl.Trim().Split(',');
|
|
foreach (var item in strs)
|
|
{
|
|
string filePath = string.Empty;
|
|
string physicalpath = ConfigurationManager.AppSettings["localRoot"];
|
|
//HttpContext.Current.Request.PhysicalApplicationPath;
|
|
filePath = physicalpath + item;
|
|
if (File.Exists(filePath))
|
|
{
|
|
fileContext.Add(fileConvertByte(item));
|
|
}
|
|
}
|
|
}
|
|
return fileContext;
|
|
}
|
|
|
|
|
|
#region 二进制数据转换为附件
|
|
/// <summary>
|
|
/// 二进制数据转换为附件
|
|
/// </summary>
|
|
/// <param name="data">二进制数据</param>
|
|
/// <param name="fileName">附件.后缀</param>
|
|
/// <param name="savePath">保存路径</param>
|
|
/// <returns>保存的相对路径</returns>
|
|
public static string ByteConvertFile(byte[] data, string fileName, string savePath)
|
|
{
|
|
string physicalpath = (ConfigurationManager.AppSettings["localRoot"] + "\\" + savePath).Replace("\\\\", "\\");
|
|
if (!System.IO.Directory.Exists(savePath))
|
|
{
|
|
Directory.CreateDirectory(savePath);
|
|
}
|
|
|
|
savePath += fileName;
|
|
FileStream fs;
|
|
if (System.IO.File.Exists(savePath))
|
|
{
|
|
fs = new FileStream(savePath, FileMode.Truncate);
|
|
}
|
|
else
|
|
{
|
|
fs = new FileStream(savePath, FileMode.CreateNew);
|
|
}
|
|
BinaryWriter br = new BinaryWriter(fs);
|
|
br.Write(data, 0, data.Length);
|
|
br.Close();
|
|
fs.Close();
|
|
|
|
return savePath;
|
|
}
|
|
#endregion
|
|
|
|
#region 附件转换二进制数据(用于保存数据库)
|
|
/// <summary>
|
|
/// 附件转换二进制数据(用于保存数据库)
|
|
/// </summary>
|
|
/// <param name="filePath">附件路径</param>
|
|
/// <returns>二进制</returns>
|
|
public static byte[] fileConvertByte(string filePath)
|
|
{
|
|
string physicalpath = (ConfigurationManager.AppSettings["localRoot"] + "\\" + filePath).Replace("\\\\", "\\");
|
|
byte[] bytContent = null;
|
|
System.IO.FileStream fs = null;
|
|
System.IO.BinaryReader br = null;
|
|
try
|
|
{
|
|
fs = new FileStream(physicalpath, System.IO.FileMode.Open);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
br = new BinaryReader((Stream)fs);
|
|
bytContent = br.ReadBytes((Int32)fs.Length);
|
|
fs.Dispose();
|
|
return bytContent;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|