我使用IFormFile时错过了一个结束括号

Dot*_*cer 3 asp.net-web-api

我正在使用ASP.NET Core创建API请求,以使用此操作为每个公司上传头像

  [HttpPost("{company_id}/updateLogo")]
    [RequestFormSizeLimitAttribute(valueCountLimit: 147483648)] 
    public async Task<IActionResult> updateCompanyLogo(IFormFile imgfile,int company_id)
    {
        string imageName;
        // upload file
        if (imgfile == null || imgfile.Length == 0)
            imageName = "default-logo.jpg";
        else
        {
            imageName = Guid.NewGuid() + imgfile.FileName;
            var path = _hostingEnvironment.WebRootPath + $@"\Imgs\{imageName}";
            if (imgfile.ContentType.ToLower().Contains("image"))
            {
                using (var fileStream = new FileStream(path, FileMode.Create))
                {
                    await imgfile.CopyToAsync(fileStream);
                }
            }
        }
        using (var db = new AppDb())
        {
            await db.Connection.OpenAsync();
            var query = new CompanyModel(db);
            var result = await query.FindOneAsync(company_id);
            if (result == null)
                return NotFound();

            result.logo = imageName;
            await result.UpdateAsync();
            return Ok(result);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我正在使用邮件发送上传请求,如 http://i.imgur.com/JvqKAiU.png

它返回

ArgumentException:键'M C hC } jIm6t(4 c ^ X X )j????np??-?60?G????e???? z 6 4 9 aa![ܢ T'是无效的JQuery语法,因为它缺少一个结束括号.参数名称:key NormalizeJQueryToMvc

Bru*_*nes 7

我昨天使用Postman将文件发送到ASPNET Core Api时遇到了同样的问题.

在我的情况下,我忘了删除Content-type标头,因此,邮递员试图将其作为JSON(或类似的东西)发送.

当我删除标题属性时,一切都按预期工作.