简体   繁体   中英

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

I tried to test uploading an IFormFile through postman to web API and the error shows the IFormFile is a null value.

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

The respsonse shows that my IFromFile is a null value but I am not able to find out where did I miss.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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