.net core api 文件上传

 

 

public class FilesController : LedaoControllerBase
{
private Imagepath imagepath;
public FilesController(Imagepath imagepath)
{
this.imagepath = imagepath;
}
#region MyRegion

// GET: api/Files/5
[HttpPost("UplodeFileOne1"), AllowAnonymous]
public async Task<AjaxResponse> UplodeFile([FromForm]IFormFile fromFile)
{
var result = ToolHelp.UplodeFile(fromFile);
return new AjaxResponse();
}
#endregion

// 本地上传
[HttpPost("UplodeFileOne"), AllowAnonymous]
public async Task<AjaxResponse> UplodeFile01([FromForm] IFormFileCollection files)
{
try
{
long size = files.Sum(f => f.Length);
var temp = "Upload/" + DateTime.Now.ToString("yyyy/MMdd");
var fileFolder = Path.Combine(imagepath.Url, temp);
if (!Directory.Exists(fileFolder))
Directory.CreateDirectory(fileFolder);
List<string> list = new List<string>();
foreach (var file in files)
{
if (file.Length > 0)
{
var fileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") +
Path.GetExtension(file.FileName);
var filePath = Path.Combine(fileFolder, fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
list.Add(temp + "/" + fileName);
}
}
var result = new { Code = 2000, Msg = "成功", Data = list };
return new AjaxResponse(result);
}
catch (Exception e)
{
return new AjaxResponse(new { Code = 2000, Msg = $"失败:{e.Message}", Data = new { } });

}
}
}

posted @ 2019-12-24 10:37  两台显示器的人生  阅读(1684)  评论(0编辑  收藏  举报