Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在上载到服务器asp.net core之前更改文件名_C#_Asp.net Mvc_File Upload_Asp.net Core Mvc - Fatal编程技术网

C# 在上载到服务器asp.net core之前更改文件名

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

您好,我想在上传到服务器和存储路径之前更改文件名 在数据库中。这是我的密码。但它会上传同名的文件。我 要更改名称(使用guid)。我该怎么做


这是我使用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
我也会编辑我的答案