简体   繁体   English

在 asp.net 核心 2.2 上上传文件 API 时不能使用替换 function

[英]can't use replace function when upload files API on asp.net core 2.2

I work on ASP.NET Core 2.2 Web API and face an issue: I can't use replace function to change the name property of a selected file that I get when uploaded.我在 ASP.NET Core 2.2 Web API 上工作,遇到一个问题:我无法使用 replace function 更改上传时获得的选定文件的名称属性。

When I try like this:当我这样尝试时:

string fileName = DisplayFileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";

I get an error我得到一个错误

Iform file doesn't contain definition for replace and no accessible extension method Replace accepting first argument of iformfile Iform 文件不包含替换定义并且没有可访问的扩展方法 Replace 接受 iformfile 的第一个参数

Full sample is here:完整样本在这里:

[HttpPost, DisableRequestSizeLimit]
public IActionResult Upload()
{
        try
        {
            var DisplayFileName = Request.Form.Files[0];
            string fileName = DisplayFileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";
            string Month = DateTime.Now.Month.ToString();
            string DirectoryCreate = myValue1 + Month;

            Path.Combine(Directory.GetCurrentDirectory(), folderName);

            if (!Directory.Exists(DirectoryCreate))
            {
                Directory.CreateDirectory(DirectoryCreate);
            }

            if (DisplayFileName.Length > 0)
            {
                var filedata = ContentDispositionHeaderValue.Parse(Request.Form.Files[0].ContentDisposition).FileName.Trim('"');
                var dbPath = Path.Combine(DirectoryCreate, fileName);
              
                using (var stream = new FileStream(dbPath, FileMode.Create))
                {
                    Request.Form.Files[0].CopyTo(stream);
                }

                return Ok(new { dbPath });
            }
            else
            {
                return BadRequest();
            }
        }
        catch (Exception ex)
        {
            return StatusCode(500, $"Internal server error: {ex}");
        }
}

How to solve this issue?如何解决这个问题? sample样本

suppose i select file developed.xlsx假设我 select 文件 developed.xlsx

then after use replace or any way result will be然后在使用替换或任何方式后结果将是

developed-sddfn78888.xlsx开发-sddfn78888.xlsx

You can use System.IO.Path to get filename and get file extension from request files.您可以使用System.IO.Path从请求文件中获取文件名和文件扩展名。 Change this string fileName = DisplayFileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";更改此string fileName = DisplayFileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx"; To

string filename = Path.GetFileName(DisplayFileName.FileName);
string fileExtension = Path.GetExtension(DisplayFileName.FileName);
string newFileName = $"{filename}-{Guid.NewGuid().ToString()}{fileExtension}";

Otherwise, you could modify your code to否则,您可以将代码修改为

 string fileName = DisplayFileName.FileName.Replace(".xlsx", "-") + Guid.NewGuid().ToString() + ".xlsx";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM