Data For Each File - Cool Coders: Quicktips

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

ASP.net Core Upload Files and Json Data for Each file - Cool Coders about:reader?url=https://doumer.me/asp-net-core-upload-files-and-json...

doumer.me

ASP.net Core Upload Files and Json


Data for Each file - Cool Coders
4-5 minutes

.Net Core , Asp.net core , C# ,

Tags: asp.net core, QuickTips

31 Jul

Hello friends, today we will solve a little problem which a few people
might encounter while building APIs in ASP.net core. This will be a
very short post, since we will go straight to the point with the
solution. We will see how to in ASP.net core upload files and json
data for each file

Why ?

You might come across a situation where you need to build an API
to upload multiple files, and allow the users of the API to pass
additional information associated to each of those files to your API.
This post tells exactly how to do that.

How ?

To upload a single file, it is quite easy. To upload multiple files too.


Now we want to upload files and json data for each file. To do that,
follow these steps.

First, define the information you want to associate to each file


uploaded via your API. In our case, this will be the attributes
corresponding to each.

public class MyFileAttributes

public double XAxis { get; set; }

public double YAxis { get; set; }

1 of 3 11/28/2020, 4:11 PM
ASP.net Core Upload Files and Json Data for Each file - Cool Coders about:reader?url=https://doumer.me/asp-net-core-upload-files-and-json...

Second, define a model binder which will be in charge of binding


the json data (we send associated to the files we upload) to the list
of C# objects passed to the controller’s action.

1 public class FormDataJsonModelBinder : IModelBinder

2 {

3 public Task BindModelAsync(ModelBindingContext bindingContext)

4 {

5 if (bindingContext == null)

6 {

7 throw new ArgumentNullException(nameof(bindingContext));

8 }

9 var valueProviderResult =

10 bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

11 if (valueProviderResult != ValueProviderResult.None)

{
12

13 bindingContext.ModelState.SetModelValue(bindingContext.ModelName
valueProviderResult);
14
var stringValue = valueProviderResult.FirstValue;
15
var result =
16
Newtonsoft.Json.JsonConvert.DeserializeObject(stringValue,
17 bindingContext.ModelType);

18 if (result != null)
19 {
20 bindingContext.Result = ModelBindingResult.Success(result);
21 return Task.CompletedTask;

22 }
23 }
24 return Task.CompletedTask;
25 }
26

2 of 3 11/28/2020, 4:11 PM
ASP.net Core Upload Files and Json Data for Each file - Cool Coders about:reader?url=https://doumer.me/asp-net-core-upload-files-and-json...

27 }

Create the controller method which will be in charge of uploading


the files and and their information. Notice that we add the
[FromForm] attribute to our list of file attributes.

public async Task<IActionResult> UploadFiles(

[FromForm][ModelBinder(BinderType =
typeof(FormDataJsonModelBinder))] IList<MyFileAttributes>
fileAttributes,

IList<IFormFile> files)

Upload the files and the json data: To do this, you will create a post
request with a form. The key in the form which takes the file
information will be named fileAttributes as what you can see
above. Then, here is an example of json data sent alongside the
files we upload.

[{ "XAxis " : 70, "YAxis" : 347}, { "XAxis " : 10, "YAxis" : 200}]

If you like this post, don’t hesitate to follow me on Twitter or


Github and subscribe to this page’s notifications to stay
updated with new articles or like my page on Facebook.

Conclusion

With this, you will be able to send files to your API and add
information to them by simply passing an array of json data. This
solution targets a small problem, but might be useful to a large
number of people.

References

https://stackoverflow.com/questions/41367602/upload-files-and-
json-in-asp-net-core-web-api

ASP.net Core Upload Files and Json Data For each file.

Follow me on social media and stay updated

3 of 3 11/28/2020, 4:11 PM

You might also like