c# - Change Content-Disposition to open all image file files in a new tab -
here's logic downloading file.
it automatically downloads file, whether it's .jpg, .pdf, .zip or whatever, open in new tab if it's image file(jpg, png, etc.) , else prompts download.
i feel i'm missing when comes solving this. :/
public void downloadarchivedfiles(archivetype type, object id, string filename) { response.contenttype = "application/zip"; response.addheader("content-disposition", "filename=" + filename); string path = server.mappath(string.format("~/{0}files/{1}", type, id)); var fmfiles = new string[0]; var files=new string[0]; if(type==archivetype.issue) { fmfiles = _files.getissuefiles(new guid(id.tostring())).select(x => server.mappath("~" + x.filepath)).toarray(); }else if(type==archivetype.task) { fmfiles = _files.gettaskfiles(int.parse(id.tostring())).select(x => server.mappath("~" + x.filepath)).toarray(); } if (!system.io.directory.exists(path) && !fmfiles.any()) return; //string[] files = system.io.directory.getfiles(server.mappath(string.format("~/{0}files/{1}", type, id))); try { files = system.io.directory.getfiles(server.mappath(string.format("~/{0}files/{1}", type, id))); } catch (exception) { ; } using (zipfile zip = new zipfile()) { zip.addfiles(files, "/"); if(fmfiles.any()) zip.addfiles(fmfiles,"/"); zip.save(response.outputstream); } httpcontext.response.end(); }
you can't open new tab providing header in response. it's late. time response being generated, browser trying response stream same tab, , old document may have been unloaded.
instead, need change way request sent. specifically, link image must have _target="blank"
attribute. tells browser open new tab (or window, depending on user's browser settings) , request image there. don't need special response headers @ point.
you should avoid target="blank"
downloadable files (e.g. pdfs). it'll work ok on browsers, on others you'll end unsightly zombie tabs.
Comments
Post a Comment