Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to download Excel file when call from Postman

I am trying to download Excel file using web API but I am unable to download file in postman where as I am able to download Excel file when I enter URL in browser though while opening file I get warning message like below :

enter image description here

enter image description here

enter image description here

When i hit endpoint using POSTMAN then file get corrupted and it is showing junk characters.

Code :

protected virtual byte[] ExportToXlsx<T>(IEnumerable<T> itemsToExport)
        {
            using (var stream = new MemoryStream())
            {
                using (var xlPackage = new ExcelPackage())
                {
                    // get handles to the worksheets
                    var worksheet = xlPackage.Workbook.Worksheets.Add(typeof(T).Name);

                    //create Headers and format them 
                    var manager = new PropertyManager<T>(itemsToExport.First());
                    manager.WriteCaption(worksheet, SetCaptionStyle);

                    var row = 2;

                    foreach (var items in itemsToExport)
                    {
                        manager.CurrentObject = items;
                        manager.WriteToXlsx(worksheet, row++, false);
                    }

                    xlPackage.Save();
                }
                return stream.ToArray();
            }
        }



private readonly IServiceContext ctx;
public void Download(string guid)
{
   var bytes = ExportToXlsx(list);
   ctx.reqobj.HttpContext.Response.Headers.Add("Content-Disposition", "attachment; filename=\"demo.xlsx\"");
   ctx.reqobj.HttpContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
   ctx.reqobj.HttpContext.Response.Body.Write(bytes, 0, bytes.Length);
}

Note : I am using OfficeOpenXml for Excel file creation.

I will appreciate any help.

Update : enter image description here

like image 565
ILoveStackoverflow Avatar asked Jun 11 '18 15:06

ILoveStackoverflow


1 Answers

Try using "Send and download" instead of "Send"

https://www.getpostman.com/docs/v6/postman/sending_api_requests/responses

like image 69
Antonio Campagnaro Avatar answered Sep 22 '22 19:09

Antonio Campagnaro