附件NAS上传测试

dev
cjy 3 weeks ago
parent 5f827d3e01
commit bf2f6b8b4a

@ -55,5 +55,13 @@ namespace DS.WMS.Core.Op.Interface
/// <param name="id"></param>
/// <returns></returns>
public DataResult<OpFileDownLoadRes> GetOpFileDownLoad(string id);
/// <summary>
/// 添加附件-测试
/// </summary>
/// <param name="formCollection"></param>
/// <param name="req"></param>
/// <returns></returns>
public Task<DataResult<string>> AddMultiFilesTest(IFormCollection formCollection, [FromForm] OpFileReq req);
}
}

@ -18,6 +18,7 @@ using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
@ -186,6 +187,91 @@ namespace DS.WMS.Core.Op.Method
return await Task.FromResult(DataResult<string>.Success(string.Join(",",filesPath.ToArray())));
//return await Task.FromResult(DataResult<string>.Success(fileRelaPath));
}
/// <summary>
/// 添加附件-测试
/// </summary>
/// <param name="formCollection"></param>
/// <param name="req"></param>
/// <returns></returns>
public async Task<DataResult<string>> AddMultiFilesTest(IFormCollection formCollection, [FromForm] OpFileReq req)
{
var tenantDb = saasService.GetBizDbScopeById(user.TenantId);
//文件集合
FormFileCollection fileCollection = (FormFileCollection)formCollection.Files;
//未上传文件
if (fileCollection == null || fileCollection.Count == 0)
{
return await Task.FromResult(DataResult<string>.Failed("附件不存在!"));
}
var limitFiles = AppSetting.app<string>(new string[] { "FileSettings", "FileType" });
//var basePath = AppSetting.app(new string[] { "FileSettings", "BasePath" });
var basePath = @"\\\\66a1249094-ckl94.cn-qingdao.nas.aliyuncs.com\\myshare\\ds8WebFiles";
var relativePath = AppSetting.app(new string[] { "FileSettings", "RelativePath" });
var filesPath = new List<string>();
// 用户名和密码
string username = "administrator";
string password = "Dctz989898";
// 使用Windows凭据进行身份验证
NetworkCredential credentials = new NetworkCredential(username, password);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(basePath), "Basic", credentials);
foreach (IFormFile file in fileCollection)
{
var originalFilename = file.FileName; // 文件原始名称
var fileSuffix = Path.GetExtension(file.FileName).ToLower(); // 文件后缀
if (!limitFiles.Contains(fileSuffix))
{
return await Task.FromResult(DataResult<string>.Failed("不允许的文件类型!"));
}
var dirAbs = string.Empty;
var fileRelaPath = string.Empty;
var fileAbsPath = string.Empty;
if (string.IsNullOrEmpty(basePath))
{
dirAbs = Path.Combine(_environment.WebRootPath, relativePath);
}
else
{
dirAbs = Path.Combine(basePath, relativePath);
}
if (!Directory.Exists(dirAbs))
Directory.CreateDirectory(dirAbs);
// 先存库获取Id
var id = SnowFlakeSingle.Instance.NextId();
var fileSaveName = $"{id}{fileSuffix}".ToLower();
fileRelaPath = Path.Combine(relativePath, fileSaveName).ToLower();
fileAbsPath = Path.Combine(dirAbs, fileSaveName).ToLower();
var newFile = new OpFile
{
Id = id,
FileName = originalFilename,
FilePath = fileSaveName,
TypeCode = req.TypeCode,
TypeName = req.TypeName,
LinkId = req.LinkId,
FileSize = file.Length.ToInt(),
FileType = Path.GetExtension(originalFilename),
Extension = Path.GetExtension(originalFilename),
Note = req.Note,
};
await tenantDb.Insertable(newFile).ExecuteCommandAsync();
using (var stream = File.Create(fileAbsPath))
{
await file.CopyToAsync(stream);
}
filesPath.Add(fileSaveName);
}
return await Task.FromResult(DataResult<string>.Success(string.Join(",", filesPath.ToArray())));
//return await Task.FromResult(DataResult<string>.Success(fileRelaPath));
}
/// <summary>
/// 获取业务附件
/// </summary>

@ -122,5 +122,17 @@ public class OpFileController : ApiController
};
}
/// <summary>
/// 多附件上传-测试
/// </summary>
/// <param name="formCollection">文件信息</param>
/// <param name="req">业务附件请求实体</param>
/// <returns></returns>
[HttpPost]
[Route("AddMultiFilesTest")]
public async Task<DataResult<string>> AddMultiFilesTest(IFormCollection formCollection, [FromForm] OpFileReq req)
{
var res = _invokeService.AddMultiFilesTest(formCollection, req);
return await res;
}
}
Loading…
Cancel
Save