How to download excel file using mvc 4 web api

I have created a execl file using aspose.cell dll and using following code to download excel file from asp.net web api.

Stream responseStream = new System.IO.MemoryStream();

workbook.Save(responseStream, SaveFormat.Xlsx);

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

response.Content = new StreamContent(responseStream);

response.Content.Headers.ContentType =new MediaTypeHeaderValue ("application/octet-stream");

response.Content.Headers.ContentDisposition =

new ContentDispositionHeaderValue("attachment");

return response;

but it downloads the corrupted file..

Thanks in advance for your suggestion...

return response;

Hi Ram,


Thank you for considering Aspose APIs.

Please consider using the overloaded version of Aspose.Cells for .NET that can accept objects of HttpResponse & ContentDisposition as demonstrated below. Moreover, please confirm that there is no problem in your logic to create/manipulate the spreadsheet by saving the Workbook directly on the disk. In case the problem persists, please provide us a sample standalone application for our review.

C#

//Creating an Workbook object
Workbook workbook = new Workbook(FileFormatType.Xlsx);
//… Your code goes here

//Save in xlsx format and send the file to user so that he may open the file in
//some application or save it to some location
workbook.Save(this.Response, “Report.xlsx”, ContentDisposition.Attachment, new OoxmlSaveOptions());
Response.End();

There is no issue saving file on the disk. I am creating a rest web api to download excel file..my controller file is inherited from ApiController class and I am not able to use httpResponse object directly in my controller..that's why I am not able to use suggested code..

To workaround I have written follwing code.. Please let me know is that is best way or not..

string filePath = HttpRuntime.AppDomainAppPath + "/Content/TempFiles/Report.Xlsx";
workbook.Save(filePath, SaveFormat.Xlsx);

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(filePath, FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Report.xlsx"
};

return response;string filePath = HttpRuntime.AppDomainAppPath + "/Content/TempFiles/Report.Xlsx";
workbook.Save(filePath, SaveFormat.Xlsx);

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(filePath, FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Report.xlsx"
};

return response;

Hi Ram,


Thank you for elaborating the requirements. Please allow us some time to look further into this matter, and get back to you with updates in this regard.

Hi again,


Here are a few tips for you.

1) In MVC applications this.Response is a HttpResponseBase type rather than a HttpResponse type so you can replace the this.Response with System.Web.HttpContext.Current.Response in my previously proposed solution.
2) I have attached a sample application using the System.Web.Mvc.FileStreamResult to send the spreadsheet on client machine.
3) If you wish to open an Excel file from the browser using a controller, please consider the following snippet.

C#

MemoryStream stream = new MemoryStream();
workbook.Save(stream, FileFormatType.Excel97To2003);
WriteStream(stream, resultFileName);


private static void WriteStream(MemoryStream memoryStream, string excelFileName)
{
HttpContext context = System.Web.HttpContext.Current;
context.Response.Clear();
context.Response.AddHeader(“content-disposition”, String.Format(“attachment;filename={0}”, excelFileName));
memoryStream.WriteTo(context.Response.OutputStream);
memoryStream.Close();
context.Response.End();
}

Thanks Babar for your support.. But attached solution is not going to make my life easy.

As I mentioned earlier I want to create web api (rest service). I am attachating my POC.. Plesae give a look.

Hi Ram,


I am sorry to know that the none of the proposed solutions are worth trying. Please attach your demo application along with a project consuming the service so we could test all possibilities on our side before getting back to you.

By the way, you have mentioned sample project from point 2) only. Have you tried other options (1 & 3)?

Hi,


sorry for the desagrement i’ve the same problem and i would know if you have found an issue

Best Regard

Hi Dimitri,


We humbly request you to please create a new thread and provide detailed problem description along with sample application (and dependencies). We will thoroughly analyze your case and get back with updates.