asp.net - C# Prompt for open/save zip in ASP receiving error -
let me start off saying i'm sure that's quite simple, unfortunately can't seem figure out. here's problem, query database, return need, zip up, , prompt user save. when prompt appears, appears system.io.compression.ziparchive.zip file name. when attempt open this, says file invalid. appreciated!!
if (e.commandname == "downloadattachment") { e.canceled = true; // create zip , send client. //response.write(@"<script language='javascript'>alert('details saved successfully')</script>"); var item = e.item grideditableitem; fileid = (int)item.getdatakeyvalue("unique"); filedata[] allrecords = null; using ( sqlconnection conn = new sqlconnection(configurationmanager.connectionstrings["ptcdbmodelentities"].connectionstring)) { using ( sqlcommand comm = new sqlcommand("select unique1, binarydata, filename ptcdbtracker.dbo.caffiletable unique1=@fileid , filename not null", conn)) { comm.parameters.add(new sqlparameter("@fileid", fileid)); conn.open(); using (var reader = comm.executereader()) { var list = new list<filedata>(); while (reader.read()) { list.add(new filedata { unique1 = reader.getint32(0) }); long len = reader.getbytes(1, 0, null, 0, 0); byte[] buffer = new byte[len]; list.add(new filedata { binarydata = (byte)reader.getbytes(1, 0, buffer, 0, (int)len), filename = reader.getstring(2) }); allrecords = list.toarray(); } } conn.close(); } } using (var compressedfilestream = new memorystream()) { //create archive , store stream in memory. using (var ziparchive = new ziparchive(compressedfilestream, ziparchivemode.update, false)) { if (allrecords != null) { foreach (var record in allrecords) { //create zip entry each attachment if (record.filename != null) { var zipentry = ziparchive.createentry(record.filename); //get stream of attachment using (var originalfilestream = new memorystream(record.binarydata)) { using (var zipentrystream = zipentry.open()) { //copy attachment stream zip entry stream originalfilestream.copyto(zipentrystream); } } } } } response.clearcontent(); response.clearheaders(); response.binarywrite(compressedfilestream.toarray()); response.appendheader("content-disposition", "attachment; filename=result.zip"); response.flush(); response.close(); ziparchive.dispose(); //how prompt open or save? } }
edit: included sami's suggestion. zip folder saves , opens, not have content.
you send correct content-disposition
header, filename append object, platform calls tostring()
on , default output class name. want send either random filename, or static one.
response.appendheader("content-disposition", "attachment; filename=result.zip");
then sending of file. use memorystream
save zip archive , don't send it. 1 way be
response.binarywrite(compressedfilestream.toarray()); response.flush(); response.close();
Comments
Post a Comment