bugfix:修复附件bug
This commit is contained in:
@@ -17,6 +17,8 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Furion.RemoteRequest.Extensions;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
|
||||
namespace Ewide.NbzsZheliban.Service
|
||||
{
|
||||
@@ -26,11 +28,12 @@ namespace Ewide.NbzsZheliban.Service
|
||||
private readonly ISqlSugarRepository repository;
|
||||
private readonly SqlSugarClient db;
|
||||
private readonly IJsonSerializerProvider _jsonSerializer;
|
||||
public DataService(ISqlSugarRepository sqlSugarRepository, IJsonSerializerProvider jsonSerializer)
|
||||
public DataService(ISqlSugarRepository sqlSugarRepository, IJsonSerializerProvider jsonSerializer, IHostingEnvironment hostingEnvironment)
|
||||
{
|
||||
repository = sqlSugarRepository;
|
||||
db = repository.Context;
|
||||
_jsonSerializer = jsonSerializer;
|
||||
this._hostingEnvironment = hostingEnvironment;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -330,22 +333,26 @@ namespace Ewide.NbzsZheliban.Service
|
||||
throw Oops.Oh("ID错误");
|
||||
}
|
||||
if (string.IsNullOrEmpty(XyFile))
|
||||
throw Oops.Oh("未上传附件");
|
||||
throw Oops.Oh(-2001, "无附件");
|
||||
var pics = new List<string>();
|
||||
var nbzs_file_path = App.Configuration["nbzs_file_path"];
|
||||
//var nbzs_file_path = App.Configuration["nbzs_file_path"];
|
||||
var nbzs_domain = App.Configuration["nbzs_domain"];
|
||||
var filePath = nbzs_file_path + XyFile;
|
||||
var current_domain = App.Configuration["current_domain"];
|
||||
|
||||
var filePath = GetCurrentRootPath() + XyFile;
|
||||
var pdfFile = new FileInfo(filePath);
|
||||
if (File.Exists(pdfFile.Directory + "\\lock"))
|
||||
{
|
||||
var picCount = pdfFile.Directory.GetFiles(pdfFile.Name + "-*.jpg").Length;
|
||||
for (int i = 0; i < picCount; i++)
|
||||
{
|
||||
pics.Add(nbzs_domain + XyFile + "-" + i + ".jpg");
|
||||
pics.Add(current_domain + XyFile + "-" + i + ".jpg");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var s = (nbzs_domain + XyFile).GetAsStreamAsync().Result;
|
||||
StreamToFile(s, XyFile);
|
||||
MagickReadSettings settings = new MagickReadSettings();
|
||||
settings.Density = new Density(400, 400); //设置质量
|
||||
using (MagickImageCollection images = new MagickImageCollection())
|
||||
@@ -359,7 +366,7 @@ namespace Ewide.NbzsZheliban.Service
|
||||
image.Format = MagickFormat.Jpg;
|
||||
var imagename = filePath + "-" + i + ".jpg";
|
||||
image.Write(imagename);
|
||||
pics.Add(nbzs_domain + XyFile + "-" + i + ".jpg");
|
||||
pics.Add(current_domain + XyFile + "-" + i + ".jpg");
|
||||
if (i == 0)
|
||||
{
|
||||
File.WriteAllText(pdfFile.Directory.FullName + "\\lock", "lock");
|
||||
@@ -368,11 +375,47 @@ namespace Ewide.NbzsZheliban.Service
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw Oops.Oh(ex.Message);
|
||||
//throw Oops.Oh(ex.Message);
|
||||
throw Oops.Oh(-2002, "无附件文件[" + nbzs_domain + XyFile + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
return new { pics, pdf = nbzs_domain + XyFile };
|
||||
return new { pics, pdf = current_domain + XyFile };
|
||||
}
|
||||
private readonly IHostingEnvironment _hostingEnvironment;
|
||||
private string GetCurrentRootPath()
|
||||
{
|
||||
return _hostingEnvironment.WebRootPath;
|
||||
//return Path.GetDirectoryName(this.GetType().Assembly.Location);
|
||||
}
|
||||
private string StreamToFile(Stream s, string filename)
|
||||
{
|
||||
var d = GetCurrentRootPath() + filename;
|
||||
FileInfo f = new(d);
|
||||
if (!f.Directory.Exists)
|
||||
f.Directory.Create();
|
||||
using (MemoryStream stmMemory = new MemoryStream())
|
||||
{
|
||||
byte[] buffer = new byte[s.Length];
|
||||
int i;
|
||||
//将字节逐个放入到Byte中
|
||||
while ((i = s.Read(buffer, 0, buffer.Length)) > 0)
|
||||
{
|
||||
stmMemory.Write(buffer, 0, i);
|
||||
}
|
||||
var fileBytes = stmMemory.ToArray();//文件流Byte,需要文件流可直接return,不需要下面的保存代码
|
||||
stmMemory.Close();
|
||||
using (MemoryStream m = new(fileBytes))
|
||||
{
|
||||
using (FileStream fs = new(d, FileMode.OpenOrCreate))
|
||||
{
|
||||
m.WriteTo(fs);
|
||||
fs.Close();
|
||||
}
|
||||
m.Close();
|
||||
}
|
||||
}
|
||||
return d;
|
||||
}
|
||||
/// <summary>
|
||||
/// 分布评估详细
|
||||
|
||||
Reference in New Issue
Block a user