c# - Response.end doesn't work -


i try download .xlsx documnent using response.end() , openxml. nothing happens. it's stuck in response.end()

public void downloadstudents(int instituteid, int departmentid) {       var memorystream = new memorystream();       using (var spreadsheetdocument = spreadsheetdocument.create(memorystream, spreadsheetdocumenttype.workbook))      {          //some code          foreach (var line in users)          {               // fill          }           spreadsheetdocument.workbookpart.workbook.save();      }       var excelname = "title.xlsx";      response.clear();     response.cachecontrol = "private";      response.contenttype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; response.addheader("content-type", "application/octet-stream");     response.appendheader("content-disposition",string.format("attachment; filename={0}; size={1}", excelname, memorystream.length));     response.binarywrite(memorystream.toarray());      **//problem here.**       response.end(); // in line nothing happens } 

the same problem appears when try use response.flush(). if use completerequest() instead response.end() .xlsx file empty

this works me files:

    public static void outputfile(filecontentresult file, string contentdisposition)     {         var context = system.web.httpcontext.current;         file.filedownloadname = contentdisposition;         context.response.clear();         context.response.clearcontent();         context.response.clearheaders();         context.response.appendheader("content-disposition", contentdisposition);         context.response.contentencoding = system.text.encoding.utf8;         context.response.contenttype = file.contenttype;         context.response.appendheader("content-length", file.filecontents.length.tostring());         context.response.binarywrite(buffer: file.filecontents.toarray());         context.response.flush();         context.applicationinstance.completerequest();     } 

the filecontentresult result of using file method of controller class in mvc.

i believe missing closing workbook before trying output it.

i suggest adding spreadsheetdocument.close() followed memorystream.flush() code before trying output view.

the contentdisposition string.format("attachment;filename={0}", filename).

also, response.end() throws error, use other one.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -