36 lines
1.6 KiB
Plaintext
36 lines
1.6 KiB
Plaintext
public async Task<IActionResult> FileSave()
|
|
{
|
|
var files = Request.Form.Files;
|
|
long size = files.Sum(f => f.Length);
|
|
string webRootPath = _hostingEnvironment.WebRootPath;
|
|
string contentRootPath = _hostingEnvironment.ContentRootPath;
|
|
|
|
List<Tuple<string, string, string>> result = new List<Tuple<string, string, string>>();
|
|
|
|
foreach (var formFile in files)
|
|
{
|
|
if (formFile.Length > 0)
|
|
{
|
|
string fileExt = Path.GetExtension(formFile.FileName);
|
|
|
|
long fileSize = formFile.Length; //获得文件大小,以字节为单位
|
|
string newFileName = formFile.FileName.Replace(fileExt, "") + '_' + System.Guid.NewGuid().ToString() + fileExt;//随机生成新的文件名
|
|
|
|
//物理路径
|
|
var phyfilePath = $"{webRootPath}\\upload\\";
|
|
|
|
if (System.IO.Directory.Exists(phyfilePath) == false)//如果不存在就创建file文件夹
|
|
{
|
|
System.IO.Directory.CreateDirectory(phyfilePath);
|
|
}
|
|
using (var stream = new FileStream(phyfilePath + newFileName, FileMode.Create))
|
|
{
|
|
await formFile.CopyToAsync(stream);
|
|
}
|
|
//前台访问路径
|
|
var filePath = $"{Request.Host}/upload/{newFileName}";
|
|
result.Add(Tuple.Create(formFile.FileName, fileExt, filePath));
|
|
}
|
|
}
|
|
return Ok(new { files.Count, result });
|
|
} |