c# - How to download memorystream to a file? -


i'm using below sample code writing , downloading memory stream file in c#.

memorystream memorystream = new memorystream(); textwriter textwriter = new streamwriter(memorystream); textwriter.writeline("something");            byte[] bytesinstream = new byte[memorystream.length]; memorystream.write(bytesinstream, 0, bytesinstream.length); memorystream.close();           response.clear(); response.contenttype = "application/force-download"; response.addheader("content-disposition",                    "attachment; filename=name_you_file.xls"); response.binarywrite(bytesinstream); response.end(); 

i getting following error:

specified argument out of range of valid values.
parameter name: offset

what may cause?

at point in code copy data array, textwriter might not have flushed data. happen when flush() or when close() it.

see if works:

memorystream memorystream = new memorystream(); textwriter textwriter = new streamwriter(memorystream); textwriter.writeline("something");    textwriter.flush(); // added line byte[] bytesinstream = memorystream.toarray(); // simpler way of converting array memorystream.close();   response.clear(); response.contenttype = "application/force-download"; response.addheader("content-disposition", "attachment;    filename=name_you_file.xls"); response.binarywrite(bytesinstream); response.end(); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -