简体   繁体   English

如何将文件中的其他参数从 Angular 传递到 ASP.NET Core controller?

[英]How to pass other parameter with file to ASP.NET Core controller from Angular?

I have a problem, I need to pass model as first a parameter, and as the second parameter, I need to pass a file我有一个问题,我需要传递 model 作为第一个参数,作为第二个参数,我需要传递一个文件

Adding a file to a form:将文件添加到表单:

const formDate = new FormData();
formDate.append("File", this.file);

Sending data from angular:从 angular 发送数据:

this.dataService.createRestaurant(this.restaurant, formDate)
    .subscribe((data: Restaurant) => this.loadProducts(data))

Data sending service:数据发送服务:

createRestaurant(model: Restaurant, form: FormData){
  const headers = new HttpHeaders().append("Content-Disposition", "multipart/form-data")
    return this.http.post(this.url, {model, form}, {headers: headers});
  }

Controller in Asp.net core: Asp.net 内核中的 Controller:

public IActionResult Post(Restaurant restaurant, IFormFile File)
    {
       code...
    }

I tried to solve this problem for two days, the model is transferred normally, but the file is always null我试了两天解决这个问题,model传输正常,但是文件总是null

Try to change your action to this:尝试将您的操作更改为:

public IActionResult Post(RestaurantViewModel viewModel)
    {
       code...
    }

by creating a ViewModel通过创建一个 ViewModel

public RestaurantViewModel
{
public Restaurant restaurant {get; set;}
public IFormFile File {get; set;}
}

This is how you shape the data into a form这就是您将数据塑造成表格的方式

const formDate = new FormData();
formDate.append("restaurant", JSON.stringify(this.restaurant));
formDate.append("file", this.file[0], this.file[0].name);

"this.restaurant" is exactly the same data model as in Asp.Net Core “this.restaurant”与 Asp.Net Core 中的数据 model 完全相同

Accept json data as in this question: ASP.NET Core and formdata binding with file and json property在这个问题中接受 json 数据: ASP.NET Core and formdata binding with file and json property

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

相关问题 asp.net核心-如何将变量从控制器传递到过滤器 - asp.net core - How to pass variable from controller to filter 如何在asp.net核心api控制器中将url中的字符串作为参数传递 - How to pass string in the url as a parameter in asp.net core api controller 我们如何在 asp.net 内核 API controller 中上传带有其他参数的图像? - How can we upload image with other parameter in asp.net core API controller? 如何通过 Asp.Net Core controller 将参数传递给 View Component - How to pass parameter to View Component via Asp .Net Core controller ASP.NET MVC将参数从视图传递到控制器 - ASP.NET MVC pass a parameter from a view to a controller 如何将两个参数传递给 ASP.NET Core MVC 中的 controller? - How to pass two parameters to a controller in ASP.NET Core MVC? ASP.NET Core 如何在服务构建器中传递参数 - ASP.NET Core how to pass parameter in service builder 如何将参数传递给 ASP.NET Core 布局中的顶部导航栏? - How to pass a parameter to top navbar in ASP.NET Core Layout? 如何实现支持 Razor 和 Angular 的 ASP.NET Core 控制器? - How to implement an ASP.NET Core controller supporting Razor and Angular? 如何将自定义对象从异步操作过滤器传递到 ASP.net 核心中的控制器? - How do I pass an custom object from an async action filter to a controller in ASP.net core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM