2

I am new in angular and being stuck to do File-upload in angular

Technologies : Angular, ASP.Net Core, Sqlserver

I need to add file-upload feature in my Angular application where services are in ASP.Net Core and in Services fileupload datatype is in Byte[]

I have form like Below enter image description here

I need to save all data in database on click of Save button with convert file in byte[] and store in database

Thanks you Guys in advance

3
  • what have you tried in this regard? have you written any code? Commented Aug 7, 2018 at 7:39
  • This isn't the first time this question has been asked here in SO. Do your research first and try it yourself so when you ask next time it would be more specific. Commented Aug 7, 2018 at 7:47
  • @ r wank, No i did not written any code. i search many sample but its save file in particular location but i want to convert file in byte[] and store in database Commented Aug 7, 2018 at 7:55

1 Answer 1

6

you don't need to convert any thing at ui side simply you can use formData and HttpClient to send binary data(files) to api.

this is a simple example

template

<input id="file" type="file"  (change)="fileChange($event.target.files)" />

component

export class FileUploadComponenet {

constractor(private httpClient:HttpClient){}

public fileChange(files:any[]) {

    if (files && files.length > 0) {
     let file = files[0];
     let formData = new FormData();
     formData.append('file', file);
     this.httpClient.post(url,formData).subscribe(res => console.log('File Uploaded ...');
    }  

  }
}

File Upload Component in Angular

Sign up to request clarification or add additional context in comments.

1 Comment

When you do it like this, how do the other fields get sent if that is a new formdata.

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.