代码
[HttpPost]
public async Task<bool> UploadFile([FromForm] IFormCollection formData)
{
try
{
#region 本地存文件地址
string fullPath = Path.Combine(Directory.GetCurrentDirectory(), "File");
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
var files = formData.Files[0];
var newfile = "File\\" + files.FileName;
var newfullPath = Path.Combine(Directory.GetCurrentDirectory(), newfile);
if (System.IO.File.Exists(Path.GetFullPath(newfullPath)))
{
System.IO.File.Delete(Path.GetFullPath(newfullPath));
}
using var stream = new FileStream(Path.Combine(fullPath, files.FileName), FileMode.Create);
await files.CopyToAsync(stream);
stream.Close();
#endregion
return true;
}
catch (Exception ex)
{
return false;
}
}