c# 4.0 - How to send uploaded excel file to C# Post API using Angular 4 -
var fd = new formdata(); fd.append('file', data.target.files[0]); return this.http.post(url), fd).map().catch();
using angularjs it's working angular 4 when used same way it's not working.i getting disk error while uploading excel file.please me.
here example, use upload files api via formdata.
in service file upload.service.ts
need create function upload files via post method. here example function:
getuploadheaders() { let headers = new headers({'accept': 'application/json'}); headers.delete('content-type'); return headers; } addnewpost(newpost: formdata): observable<formdata> { const endpoint = 'https://yourapiurl.com'; return this.http .post(endpoint, newpost, { headers: this.getuploadheaders() }) .catch((e) => this.handleerror(e)); }
and should create upload function in component, example upload.component.ts
. here example upload function formdata.
filetoupload: file = null; constructor(private uploadservice: uploadservice) { } handlefileinput(files: filelist) { this.filetoupload = files.item(0); } savefiletoapi() { const saveform: formdata = new formdata(); if (this.filetoupload) { saveform.append('filefieldnameonyourapi', this.filetoupload, this.filetoupload.name); } this.uploadservice.addnewpost(saveform).subscribe(() => { console.log('upload success!'); }, error => { console.log('upload failed!'); }); }
for uploading file via formdata need 3 parameters: propertyname on api endpoint, file , name of file. , in upload.component.html
need have form one:
<form (ngsubmit)="onsubmit()"> <label for="filefield">chose file upload</label> <input type="file" id="filefield" name="filefield" (change)="handlefileinput($event.target.files)"> <button type="submit">speichern</button> </form>
for more detail of formdata read this , more information formdata.append() read this.
Comments
Post a Comment