附件上传
This commit is contained in:
parent
60a4bc3644
commit
582db250d5
@ -133,15 +133,16 @@ namespace MineTec.ProManager.GalleryFiles
|
|||||||
}
|
}
|
||||||
|
|
||||||
//单个文件上传
|
//单个文件上传
|
||||||
public async Task<string> UploadFilessss(IFormFile file)
|
public async Task<string> UploadFile(IFormFile file)
|
||||||
{
|
{
|
||||||
if (file == null)
|
if (file == null)
|
||||||
{
|
{
|
||||||
return "请先选择文件";
|
return "请先选择文件";
|
||||||
}
|
}
|
||||||
string webRootPath = @"D:\UploadingFiles\"; // wwwroot 文件夹
|
string webRootPath = @"D:\UploadingFiles\"; //保存文件路径
|
||||||
string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));
|
string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));//增加日期文件夹
|
||||||
string dirPath = Path.Combine(webRootPath, uploadPath);
|
string dirPath = Path.Combine(webRootPath, uploadPath);
|
||||||
|
//创建路径
|
||||||
if (!Directory.Exists(dirPath))
|
if (!Directory.Exists(dirPath))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(dirPath);
|
Directory.CreateDirectory(dirPath);
|
||||||
@ -159,33 +160,34 @@ namespace MineTec.ProManager.GalleryFiles
|
|||||||
}
|
}
|
||||||
|
|
||||||
//多个文件上传
|
//多个文件上传
|
||||||
public async Task<string> UploadPhotoszzzzz(IFormFileCollection files)
|
public async Task<string> UploadFiles(IFormFileCollection files)
|
||||||
{
|
{
|
||||||
long size = files.Sum(f => f.Length);
|
if (files.Count() == 0)
|
||||||
|
{
|
||||||
string webRootPath = @"D:\UploadingFiles\"; // wwwroot 文件夹
|
return "请先选择文件";
|
||||||
string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));
|
}
|
||||||
string fileFolder = Path.Combine(webRootPath, uploadPath);
|
string webRootPath = @"D:\UploadingFiles\"; //保存文件路径
|
||||||
|
string uploadPath = Path.Combine("uploads", DateTime.Now.ToString("yyyyMMdd"));//增加日期文件夹
|
||||||
if (!Directory.Exists(fileFolder))
|
string dirPath = Path.Combine(webRootPath, uploadPath);
|
||||||
Directory.CreateDirectory(fileFolder);
|
//创建路径
|
||||||
|
if (!Directory.Exists(dirPath))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(dirPath);
|
||||||
|
}
|
||||||
foreach (var file in files)
|
foreach (var file in files)
|
||||||
{
|
{
|
||||||
if (file.Length > 0)
|
|
||||||
{
|
|
||||||
var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") +
|
|
||||||
Path.GetExtension(file.FileName);
|
|
||||||
var filePath = Path.Combine(fileFolder, fileName);
|
|
||||||
|
|
||||||
using (var stream = new FileStream(filePath, FileMode.Create))
|
|
||||||
{
|
string fileExt = Path.GetExtension(file.FileName).Trim('.'); //文件扩展名,不含“.”
|
||||||
await file.CopyToAsync(stream);
|
string newFileName = Guid.NewGuid().ToString().Replace("-", "") + "." + fileExt; //随机生成新的文件名
|
||||||
}
|
var filePath = Path.Combine(dirPath, newFileName);
|
||||||
|
|
||||||
|
using (var stream = new FileStream(filePath, FileMode.Create))
|
||||||
|
{
|
||||||
|
await file.CopyToAsync(stream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
var url = $@"\{uploadPath}\{dirPath}";
|
||||||
var url = $@"\{uploadPath}\11";
|
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user