3

I have running Angular 9 application and I wanted to implement File upload behavior. In the form, user has to enter title, description and upload only one file in .zip format and while clicking on Submit, I want to send the form values along with files to Backend (using dotnet) via http post call.

file-upload.component.ts

        uploadFile(files) {
            const formData = new FormData();
            const fileToUpload = files[0] as File;
            formData.append('file', fileToUpload, fileToUpload.name);
            const data = {
               title: this.form.value.Title,
               description: this.form.value.Description,
               File: formData
            };
            console.log(data);
            this.http.post('https://localhost:5001/api/idea/add', data).subscribe((response) => {
                console.log(response);
            }});
        }

file-upload.component.html

<input type="file" #file placeholder="Choose file" (change)="uploadFile(file.files)" multiple>

FileController.cs

[HttpPost("api/idea/add")]
public async Task<IActionResult> AddIdea([FromBody] IdeaDto ideaDto) { }

Backend expects the data to come in below format

IdeaDto.cs

public class IdeaDto
{
    public IFormFile File { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
}

I am getting below error while submitting the data

enter image description here

Also, I did console.log(data) and got File value as shown in the below image. I am not sure whether this the correct data

enter image description here

What is wrong here? I'm really out of ideas, maybe I need a fresh thoughts on this after spending so much time

2 Answers 2

6

Take from form in api

[HttpPost("api/idea/add")]
public async Task<IActionResult> AddIdea([FromForm] IdeaDto ideaDto) { }

and in angular fill formdata rather than create object

uploadFile(files) {
            const formData = new FormData();
            const fileToUpload = files[0] as File;
            formData.append('file', fileToUpload, fileToUpload.name);
            formData.append('title',this.form.value.Title);
            formData.append('description',this.form.value.Description);
            console.log(data);
            this.http.post('https://localhost:5001/api/idea/add', formData ).subscribe((response) => {
                console.log(response);
            }});
        }
Sign up to request clarification or add additional context in comments.

Comments

0

When you post binary data like files, they have to be posted as multi-part form data, please use the below header

Content-Type: multipart/form-data;

In the model, you should receive the data as File instead of a custom interface (IFormFile)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.