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
Post a Comment