【问题标题】:IFormFile is null value when uploading file from postman to web API将文件从邮递员上传到 Web API 时,IFormFile 为空值
【发布时间】:2019-06-07 13:09:16
【问题描述】:

我尝试通过邮递员测试将 IFormFile 上传到 Web API,错误显示 IFormFile 为空值。

    [HttpPost("upload")]
    public async Task<string> Post(IFormFile photo)
    {
        try
        {
            // Full path to file in  location
            string fname = DoPhotoUpload(photo);
            string filePath = Path.Combine(_env.WebRootPath, "photos/" + fname);

            if (photo.Length > 0)
                using (var stream = new FileStream(filePath, FileMode.Create))
                    await photo.CopyToAsync(stream);
            //return Ok(new { count = 1, path = filePath });
            return fname;
        } catch (Exception ex)
        {
            return ex.Message;
        }

响应显示我的 IFromFile 是一个空值,但我无法找出我错过了哪里。

【问题讨论】:

    标签: c# post asp.net-web-api postman nullreferenceexception


    【解决方案1】:
    [HttpPost("upload")]
    public async Task<string> Post([FromBody] IFormFile photo)
    {
        try
        {
            // Full path to file in  location
            string fname = DoPhotoUpload(photo);
            string filePath = Path.Combine(_env.WebRootPath, "photos/" + fname);
    
            if (photo.Length > 0)
                using (var stream = new FileStream(filePath, FileMode.Create))
                    await photo.CopyToAsync(stream);
                //return Ok(new { count = 1, path = filePath });
            return fname;
        } catch (Exception ex)
        {
            return ex.Message;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2023-03-04
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 2019-09-29
      • 2021-04-05
      • 1970-01-01
      • 2023-02-22
      相关资源
      最近更新 更多