1
This commit is contained in:
@@ -5,11 +5,52 @@ using System.Text;
|
||||
using System.Collections;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Net;
|
||||
|
||||
namespace BLL
|
||||
{
|
||||
public static class FileInsertService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取服务器图片转byte
|
||||
/// </summary>
|
||||
/// <param name="fileHost"></param>
|
||||
/// <param name="attachUrl"></param>
|
||||
/// <returns></returns>
|
||||
public static List<byte[]> FilePathTransStream(string fileHost, string attachUrl)
|
||||
{
|
||||
List<byte[]> bytes = new List<byte[]>();
|
||||
var strs = attachUrl.Trim().Split(',');
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var fileUrl in strs)
|
||||
{
|
||||
string filepath = $"{fileHost}{fileUrl}";
|
||||
// 创建WebClient实例
|
||||
using (WebClient webClient = new WebClient())
|
||||
{
|
||||
// 下载图片并保存到内存流
|
||||
using (MemoryStream ms = new MemoryStream(webClient.DownloadData(filepath)))
|
||||
{
|
||||
// 将MemoryStream转换为byte数组
|
||||
byte[] imageBytes = ms.ToArray();
|
||||
bytes.Add(imageBytes);
|
||||
//// 使用byte数组(例如,保存到文件或进行其他处理)
|
||||
//File.WriteAllBytes("localImage.jpg", imageBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Error: " + ex.Message);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 获取附件数据流类
|
||||
/// </summary>
|
||||
@@ -19,7 +60,7 @@ namespace BLL
|
||||
{
|
||||
if (fileContextList != null && fileContextList.Count > 0)
|
||||
{
|
||||
string physicalpath = Funs.RootPath;
|
||||
string physicalpath = Funs.AttachRootPath;
|
||||
//HttpContext.Current.Request.PhysicalApplicationPath;
|
||||
string fullPath = physicalpath + attachUrl;
|
||||
if (!File.Exists(fullPath))
|
||||
@@ -61,18 +102,29 @@ namespace BLL
|
||||
{
|
||||
if (strs.Count() > i)
|
||||
{
|
||||
string physicalpath = Funs.RootPath;
|
||||
//HttpContext.Current.Request.PhysicalApplicationPath;
|
||||
string fullPath = physicalpath + strs[i];
|
||||
string physicalpath = Funs.AttachRootPath;
|
||||
string fpath = strs[i];
|
||||
string fullPath = physicalpath + fpath;
|
||||
if (!File.Exists(fullPath))
|
||||
{
|
||||
byte[] fileContext = item;
|
||||
int index = fullPath.LastIndexOf("\\");
|
||||
string filePath = fullPath.Substring(0, index);
|
||||
string fileName = Regex.Match(fullPath, @"[^/\\?]+(\?.*)?$").Value;
|
||||
string filePath = fullPath.Replace(fileName, "");
|
||||
//int index = fullPath.LastIndexOf("\\");
|
||||
//string filePath = fullPath.Substring(0, index) ;
|
||||
//string filePath = index > 0 ? fullPath.Substring(0, index) : fullPath;
|
||||
|
||||
if (!Directory.Exists(filePath))
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(filePath);
|
||||
if (!Directory.Exists(filePath))
|
||||
{
|
||||
Directory.CreateDirectory(filePath);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ErrLogInfo.WriteLog($"附件【{filePath}】获取异常!");
|
||||
//continue;
|
||||
}
|
||||
//string savePath = fullPath + fileName;
|
||||
|
||||
@@ -125,7 +177,7 @@ namespace BLL
|
||||
if (attachFile.AttachUrl != attachUrl)
|
||||
{
|
||||
///删除附件文件
|
||||
BLL.UploadAttachmentService.DeleteFile(Funs.RootPath, attachFile.AttachUrl);
|
||||
BLL.UploadAttachmentService.DeleteFile(Funs.AttachRootPath, attachFile.AttachUrl);
|
||||
////插入附件文件
|
||||
BLL.FileInsertService.FileMoreInsert(fileContext, attachUrl);
|
||||
attachFile.AttachSource = attachSource;
|
||||
@@ -134,5 +186,45 @@ namespace BLL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 数据和附件插入到多附件表【不存实际文件,只存地址】
|
||||
/// </summary>
|
||||
public static void InsertAttachFileRecord(string attachFileId, string dataId, string attachSource, string attachUrl)
|
||||
{
|
||||
var getAtt = Funs.DB.AttachFile.FirstOrDefault(x => x.AttachFileId == attachFileId);
|
||||
if (getAtt != null)
|
||||
{
|
||||
Funs.DB.AttachFile.DeleteOnSubmit(getAtt);
|
||||
Funs.DB.SubmitChanges();
|
||||
}
|
||||
//多附件
|
||||
var attachFile = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == dataId);
|
||||
if (attachFile == null && !string.IsNullOrEmpty(attachSource))
|
||||
{
|
||||
Model.AttachFile newAttachFile = new Model.AttachFile
|
||||
{
|
||||
AttachFileId = attachFileId,
|
||||
ToKeyId = dataId,
|
||||
AttachSource = attachSource,
|
||||
AttachUrl = attachUrl
|
||||
};
|
||||
Funs.DB.AttachFile.InsertOnSubmit(newAttachFile);
|
||||
Funs.DB.SubmitChanges();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (attachFile.AttachUrl != attachUrl)
|
||||
{
|
||||
///删除附件文件
|
||||
BLL.UploadAttachmentService.DeleteFile(Funs.AttachRootPath, attachFile.AttachUrl);
|
||||
attachFile.AttachSource = attachSource;
|
||||
attachFile.AttachUrl = attachUrl;
|
||||
Funs.DB.SubmitChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user