Upload Files using Web API in Angular

Last Reply 3 months ago By dharmendr

Posted 3 months ago

in angular, i want to test upload any file using asp.net web api

<input type="file" id="file" (change)="uploadfile($event.target.files)">
<button type="button" (click)="upload()" >upload</button>

 

fileToUpload: File = null;
public imagePath;


 uploadfile(files: File[]){
    if(File.length===0){
      return;
    }
    this.imagePath=files;
this.fileToUpload=files[0];
var mimeType=this.fileToUpload.type;
if(mimeType.match(/image\/*/)==null){
  this.message="only image are supported";
  return;
}
var reader = new FileReader();
reader.readAsDataURL(this.fileToUpload);
    reader.onload = (event:any) => {
      // const csv: string | ArrayBuffer = reader.result;
  //this.imagePreview = event.target.result;
  if(reader.readyState){
    this.imgUrl=reader.result;
  }
  
  
  };
  reader.readAsDataURL(this.fileToUpload);
  reader.abort();
  }
  upload(){
   
    alert(this.fileToUpload.name);
    
this.http.get('http://localhost:62409/placementing.asmx/fileuploader?byte1='+bytes'&filename='+this.fileToUpload.name+'contentType='+this.fileToUpload.type)
//  .subscribe();
}

 

Posted 3 months ago
Hi @mukesh1,
Please try the following

File size and content type warning for FileUpload using C# and VB.Net in ASP.Net

It might help you.

Cheers Andrea.

Posted 3 months ago

sorry, i want to upload in angular using asp.net webapi(.asmx), not in this way


Posted 3 months ago

Posted 3 months ago

Sir in angular not angular js with asp.net webmethod service(.asmx)


Posted 3 months ago