C# 在上载到服务器asp.net core之前更改文件名
您好,我想在上传到服务器和存储路径之前更改文件名 在数据库中。这是我的密码。但它会上传同名的文件。我 要更改名称(使用guid)。我该怎么做C# 在上载到服务器asp.net core之前更改文件名,c#,asp.net-mvc,file-upload,asp.net-core-mvc,C#,Asp.net Mvc,File Upload,Asp.net Core Mvc,您好,我想在上传到服务器和存储路径之前更改文件名 在数据库中。这是我的密码。但它会上传同名的文件。我 要更改名称(使用guid)。我该怎么做 这是我使用FileStream的上传方法,希望对你有所帮助 [HttpPost] public async Task<IActionResult> UploadFileImage(ICollection<IFormFile> files) { string file_name = "the file
这是我使用FileStream的上传方法,希望对你有所帮助
[HttpPost]
public async Task<IActionResult> UploadFileImage(ICollection<IFormFile> files)
{
string file_name = "the file name";
var uploads = Path.Combine(_environment.WebRootPath, "uploads/Image");
foreach (var file in files)
{
if (file.Length > 0)
{
using (var fileStream = new FileStream(Path.Combine(uploads, file_name ), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
}
return View();
}
[HttpPost]
公共异步任务上载文件映像(ICollection文件)
{
string file_name=“文件名”;
var uploads=Path.Combine(_environment.WebRootPath,“uploads/Image”);
foreach(文件中的var文件)
{
如果(file.Length>0)
{
使用(var fileStream=newfilestream(Path.Combine(上传,文件名),FileMode.Create))
{
等待file.CopyToAsync(fileStream);
}
}
}
返回视图();
}
这是我使用FileStream的上传方法,希望对你有所帮助
[HttpPost]
public async Task<IActionResult> UploadFileImage(ICollection<IFormFile> files)
{
string file_name = "the file name";
var uploads = Path.Combine(_environment.WebRootPath, "uploads/Image");
foreach (var file in files)
{
if (file.Length > 0)
{
using (var fileStream = new FileStream(Path.Combine(uploads, file_name ), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
}
return View();
}
[HttpPost]
公共异步任务上载文件映像(ICollection文件)
{
string file_name=“文件名”;
var uploads=Path.Combine(_environment.WebRootPath,“uploads/Image”);
foreach(文件中的var文件)
{
如果(file.Length>0)
{
使用(var fileStream=newfilestream(Path.Combine(上传,文件名),FileMode.Create))
{
等待file.CopyToAsync(fileStream);
}
}
}
返回视图();
}
我知道已经很晚了,但这可能会有所帮助。
只需将旧文件复制到新文件,并在上载文件后删除旧文件。比如:
var oldFilePath = folderPath + fileName; //filename: Uploading file
using (FileStream fileStream = System.IO.File.Create(oldFilePath))
{
await item.CopyToAsync(fileStream);
}
var newFileName = someCustomString + fileName;
var newFilePath = folderPath + newFileName;
System.IO.File.Copy(oldFilePath, newFilePath, true);
System.IO.File.Delete(oldFilePath);
我知道已经很晚了,但也许这能帮上忙。 只需将旧文件复制到新文件,并在上载文件后删除旧文件。比如:
var oldFilePath = folderPath + fileName; //filename: Uploading file
using (FileStream fileStream = System.IO.File.Create(oldFilePath))
{
await item.CopyToAsync(fileStream);
}
var newFileName = someCustomString + fileName;
var newFilePath = folderPath + newFileName;
System.IO.File.Copy(oldFilePath, newFilePath, true);
System.IO.File.Delete(oldFilePath);
您在哪里更改文件名?@Progressive我通过自定义旧文件名(已上载)创建了一个新文件名。然后我用新文件名创建同一文件夹的路径
var newFileName=someCustomString+fileName;var newFilePath=folderpath+newFileName
我也会编辑我的答案,您在哪里更改文件名?@Progressive我通过自定义旧文件名(已上载)创建新文件名。然后我用新文件名创建同一文件夹的路径var newFileName=someCustomString+fileName;var newFilePath=folderpath+newFileName
我也会编辑我的答案