【问题标题】:How to rename uploaded files asp.net core mvc如何重命名上传的文件 asp.net core mvc
【发布时间】:2017-12-26 22:02:50
【问题描述】:

我创建了一个这样的上传链接 这是我在控制器中的代码

    [HttpPost]
    public async Task<IActionResult> Index(ICollection<IFormFile> files)
    {
        var uploads = Path.Combine(_environment.WebRootPath, "UploadedFiles/Archives");
        foreach (var file in files)
        {
            if (file.Length > 0)
            {

                using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
                {
                    await file.CopyToAsync(fileStream);
                }
            }
        }
        return View();
    }

它以用户定义的名称保存文件。比如“Alumni Survey.pdf”

我想将“Alumni Survey.pdf”重命名为“2017.pdf”,我该怎么做? 除了我想限制用户只能上传.pdf文件,我应该搜索什么?

【问题讨论】:

  • 关于命名。只是不要使用file.FileName 并将其命名为您想要的任何名称
  • 你了解自己的代码吗?您将文件名传递给FileStream。只需传递一个不同的文件名。
  • 我找到了我的解决方案,这里是一个菜鸟问题。谢谢回复
  • 如果要在Archives文件夹下生成新文件夹,应该怎么做?
  • @EmilyGermanotta 和文件限制使用文件输入标签accept 中的accept 属性accept="application/pdf"

标签: c# asp.net-core upload asp.net-core-mvc rename


【解决方案1】:

我想将“Alumni Survey.pdf”重命名为“2017.pdf”,我该怎么做?

只是不要使用file.FileName 并将其命名为您想要的任何名称。

[HttpPost]
public async Task<IActionResult> Index(ICollection<IFormFile> files) {
    var uploads = Path.Combine(_environment.WebRootPath, "UploadedFiles/Archives");
    foreach (var file in files) {
        if (file.Length > 0) {
            using (var fileStream = new FileStream(Path.Combine(uploads, "<My file name here>"), FileMode.Create)) {
                await file.CopyToAsync(fileStream);
            }
        }
    }
    return View();
}

请注意,由于您拥有一组文件,因此在命名它们时需要考虑上传中的多个文件。不能全都一样。

我想限制用户只能上传.pdf文件,

对于文件限制,使用文件输入标签accept="application/pdf"中的accept属性

<input type="file" 
       class="form-control" 
       id="files" 
       name="files"
       accept="application/pdf">

【讨论】:

  • 另一个问题是我现在需要两个使用相同控制器 index() 函数的表单,我怎么知道哪个表单正在调用?
  • 在隐藏输入中添加属性/字段/表单值,您可以在操作中检查以了解表单何时发送。您现在正在向 OP 添加其他要求。不要那样做。如果您有新问题,请创建新问题。
【解决方案2】:
    public IActionResult FileUpload()
    {
        try
        {
            var file = Request.Form.Files[0];
            var folderName = Path.Combine("Resources", "Files");
            var pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);

            if (file.Length > 0)
            {
                var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                //var fullPath = Path.Combine(pathToSave, fileName);
                string renameFile = Convert.ToString(Guid.NewGuid()) + "." + fileName.Split('.').Last();
                var fullPath = Path.Combine(pathToSave, renameFile);
                var dbPath = Path.Combine(folderName, fileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }
                return Ok(new { renameFile });
            }
            else
            {
                return BadRequest();
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, "Internal server error");
        }
    }`

【讨论】:

    【解决方案3】:

    要重命名文件,请使用 Move,如下所示。对于 oldFilePath 和 newFilePath 把你以前的文件路径和新文件名的文件路径改成。

     System.IO.File.Move(oldFilePath, newFilePath);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-30
      • 2014-01-01
      • 1970-01-01
      相关资源
      最近更新 更多