c# - How to give a downloaded file unique name -



c# - How to give a downloaded file unique name -

i have c# application saves completed pdf file on folder within site. during operation save 2 session variable filename , filepath in server:

string strfilename = "completed_pdf_" + k + ".pdf"; //k variable in function name session["filename"] = strfilename; messagebox.show(session["filename"].tostring()); //displays: completed_pdf_{name}.pdf newfileserver = system.environment.machinename + @"/pdfgenerate/completed_pdf_" + k + ".pdf"; strfullpath = path.getfullpath("//" + newfileserver); list<system.web.ui.webcontrols.listitem> files = new list<system.web.ui.webcontrols.listitem>(); files.add(new system.web.ui.webcontrols.listitem(strfullpath, strfullpath)); strn = files[0].tostring(); session["pathname"] = strn; messagebox.show(session["pathname"].tostring()); //displays: \\myserver\pdfgen\completed_pdf_{name}.pdf

i have gridview displays linkbutton:

<asp:linkbutton id="lnkdownload" text = "download" runat="server" onclick = "downloadfile" />

the function linkbutton is:

protected void downloadfile(object sender, eventargs e) { //messagebox.show(session["pathname"].tostring()); //displays correctly //messagebox.show(session["filename"].tostring()); //displays correctly response.redirect("downloadfilepdf.ashx?myvar=" + session["pathname"].tostring() + "&myvar2=" + session["filename"].tostring()); }

my httphandler code this:

<%@ webhandler language="c#" class="downloadfilepdf" %> using system; using system.web; public class downloadfilepdf : ihttphandler { public void processrequest (httpcontext context) { system.web.httprequest request = system.web.httpcontext.current.request; string strsessvar = request.querystring["pathname"]; system.web.httprequest request2 = system.web.httpcontext.current.request; string strsessvar2 = request.querystring["filename"]; system.web.httpresponse response = system.web.httpcontext.current.response; response.clearcontent(); response.clear(); response.contenttype = "application/pdf"; response.addheader("content-disposition", "attachment; filename=" + strsessvar + ";"); response.end(); } public bool isreusable { { homecoming false; } } }

when run website in server itself, asks me download ashx file if run website local pc on same network server, prompts me download pdf file. far, however, running 2 issues:

the filename downloading in pc downloadfilepdf httphandler filename. the file 0 byte , when open file, not right file type.

how can prepare that..

the filename filename querystring sending httphandler file. i can download file residing in server itself, it's not 0 byte.

how give downloaded file unique name

you can utilize couple of options, guid class, datetime.now method , on in order have unique identifier downloaded file, example, utilize guid.newguid:

response.addheader("content-disposition", "attachment; filename=" + string.format(strsessvar+{0}, gui.newguid()) + ";");

update:

by using next code you're doing nil sending empty file:

system.web.httpresponse response = system.web.httpcontext.current.response; response.clearcontent(); response.clear(); response.contenttype = "application/pdf"; response.addheader("content-disposition", "attachment; filename=" + strsessvar + ";"); response.end();

in order solve it, jst stream file content response, look:

response.binarywrite(getfilecontentsfromsomewhere());

c# asp.net

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -