Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want export to excel in angular and web api using npm filesaver

In Console I am getting the error message :
The request body isn't either a blob or an array buffer

Here is my code

Web Api :
C#
public HttpResponseMessage GetExcelData()
        {

            try
            {
                JobAssignment j = new JobAssignment();
              string filePath = j.GetExcelData();

                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = Path.GetFileName(filePath) };
               // result.Content.Headers.ContentLength = stream.Length;
                result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");

                return result;
            }
            catch (Exception ex)
            {
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                return result;
            }
        }



Component :

C#
GetExcelData() {
  this.loaderService.GetExcelData()
    .subscribe(
    blob => {
      importedSaveAs(blob, "file1");
    },
    error => {
      this.errorMessage = <any>error
    });
}


service :

C#
 GetExcelData(): Observable<Blob> {
let headers = new Headers({ 'Content-Type': 'application/json' });
   let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });
     let bodyString = {};
   return this.http.get(this.rootUrl + 'GetExcelData',{responseType: ResponseContentType.Blob})
     .map(res => res.blob())
     .catch(this.handleError);
 }


What I have tried:

I have changed
C#
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/vnd.ms-excel");


But it is not working.
Please tell me where I am wrong.
Posted
Updated 15-Aug-17 23:37pm
v2

1 solution

Ok I got the solution, very simply we can do it.


Web Api

C#
public void GetExcelData()
{

    string sFileName = System.IO.Path.GetRandomFileName();
    string sGenName = "Friendly.xls";
    System.IO.FileStream fs = null;
    fs = System.IO.File.Open(HttpContext.Current.Server.MapPath("~/TextFiles/StudentWiseRegistrationReport__14082017_134859.xls"), System.IO.FileMode.Open);
    byte[] btFile = new byte[fs.Length];
    fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName);
    HttpContext.Current.Response.ContentType = "application/octet-stream";
    HttpContext.Current.Response.BinaryWrite(btFile);
    HttpContext.Current.Response.End();
}



And In Component:

C#
window.location.href='http://localhost:1036/api/Employee/GetExcelData';


Excel sheet or text file can be downloaded in this way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900