简体   繁体   English

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

[英]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. 我尝试测试通过邮递员将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;
        }

The respsonse shows that my IFromFile is a null value but I am not able to find out where did I miss. 响应显示我的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.

相关问题 从控制台应用程序接收文件时,IFormFile 始终为空 - IFormFile is always null when receiving file from console app 如何修复来自 Post 的 ASP.NET Web Api 接受的空 IFormFile - How to fix a null IFormFile being accepted by an ASP.NET Web Api from a Post 在 C# Web API 中,当我在 Postman 中发布多个数据时,它返回“null”值 - In C# Web API, when I POST multiple data in the Postman it returns 'null' value 如何在使用IFormFile上传时更改文件名 - How to change file name when uploading using IFormFile 为什么文件IFormFile = null? - Why file IFormFile = null? 当输入名称为file []时,对IFormFile的绑定不起作用。 Dropzone + .Net Core Web API - When input name is file[] the bind to IFormFile does not work. Dropzone + .Net Core Web API ASP .NET Core Web API - Getting and extracting a.zip file from upload controller, using IFormFile - ASP .NET Core Web API - Getting and extracting a .zip file from upload controller, using IFormFile Web API Null Content C# from Postman - Web API Null Content C# from Postman 将文件从WPF应用程序上传到Web API - Uploading a file from a wpf application to a web api 如何将表单数据从 javascript 发送到 web api 作为 IFormFile - How to send form data from javascript to web api as IFormFile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM