在ASP.NET Core Web API中一起接收文件和其他表单数据(基于边界的请求解析) [英] Receive file and other form data together in ASP.NET Core Web API (boundary based request parsing)

查看:991
本文介绍了在ASP.NET Core Web API中一起接收文件和其他表单数据(基于边界的请求解析)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何为应该接收一个文件和一个文本值的操作方法形成参数

How would you form your parameters for the action method which is supposed to receive one file and one text value from the request?

我尝试了这个

public string Post([FromBody]string name, [FromBody]IFormFile imageFile)

并尝试从邮递员中将其击中,但是它给出了我 500内部服务器错误。我也无法调试它,因为它永远不会碰到我将断点放在action方法中的第一条语句。

And tried hitting it from Postman but it gives me 500 Internal Server Error. I also can't debug it because it never hits the first statement inside the action method where I've put my breakpoint.

任何想法我们都不能解析基于边界的请求并提取文件和其他文本字段?我是ASP.NET Core的新手。

Any idea how can we parse boundary based requests and extract file(s) and other textual field(s)? I am new to ASP.NET Core.

推荐答案

我遇到了类似的问题,并通过使用[FromForm]属性解决了该问题。和FileUploadModelView在函数中,如下所示:

I had the similar issue and I solved the problem by using [FromForm] attribute and FileUploadModelView in the function as follow:

[HttpPost]
        [Route("upload")]
        public async Task<IActionResult> Upload([FromForm] FileUploadViewModel model, [FromForm] string member_code)
        {
            var file = model.File;

            ...
        }

这篇关于在ASP.NET Core Web API中一起接收文件和其他表单数据(基于边界的请求解析)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆