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

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

问题描述

对于应该从请求中接收一个 file 和一个 text 值的操作方法,您将如何形成参数?

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)

并尝试从 Postman 中点击它,但它给了我 500 Internal Server Error.我也无法调试它,因为它永远不会命中我放置断点的 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天全站免登陆