繁体   English   中英

将文件从邮递员上传到Web API时,IFormFile为空值

[英]IFormFile is null value when uploading file from postman to web API

我尝试测试通过邮递员将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为空值,但我无法找出我错过的地方。

[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;
    }
}

暂无
暂无

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

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