[HttpPost("[action]")]
public async Task<dynamic> NewUploadFile(IFormFile fileinput)
{
try
{
if (fileinput == null)
{
return new { code = ApiCode.Perror, message = "文件为空!" };
}
// 原文件名(包括路径)
var filename = ContentDispositionHeaderValue.Parse(fileinput.ContentDisposition).FileName;
// 扩展名
var extName = filename.Substring(filename.LastIndexOf('.')).Replace(""", "");
// 新文件名
//string shortfilename = $"{Guid.NewGuid()}{extName}";
// 新文件名(包括路径)
filename = Environment.WebRootPath + @"upload" + filename;
//判断路径是否存在
if (!Directory.Exists(Environment.WebRootPath + @"upload"))
Directory.CreateDirectory(Environment.WebRootPath + @"upload");
// 创建新文件
using (FileStream fs = System.IO.File.Create(filename))
{
// 复制文件
fileinput.CopyTo(fs);
// 清空缓冲区数据
fs.Flush();
return new { code = ApiCode.Success, message = "文件上传成功!" };
}
}
catch (Exception e)
{
Logger.LogInformation(e + "
");
return new { code = ApiCode.Exception, message = "服务器内部异常,请联系管理员!" };
}
}