c# - How to open a pdf file in a WebForm application by search? -


when click on listbox search pdf file, it's not opening.

the code below. thoughts?

protected void button1_click(object sender, eventargs e) {   listbox1.items.clear();   string search = textbox1.text;   if (textbox1.text != "")    {     string[] pdffiles = directory.getfiles(@"\\192.168.5.10\fbar\report\clotho\h2\report\", "*" + textbox1.text + "*.pdf", searchoption.alldirectories);     foreach (string file in pdffiles)     {       // listbox1.items.add(file);       listbox1.items.add(path.getfilename(file));     }   }   else   {     response.write("<script>alert('for wafer id report not generated');</script>");   } }  protected void listbox1_selectedindexchanged(object sender, eventargs e) {   string pdffiles = listbox1.selecteditem.tostring();    string.format("attachment; filename={0}", filename));    processstartinfo infoopenpdf = new processstartinfo();   infoopenpdf.filename = pdffiles;   infoopenpdf.verb = "open";   // process.start(file);   infoopenpdf.createnowindow = true;   infoopenpdf.windowstyle = processwindowstyle.normal;    process openpdf = new process();   openpdf.startinfo = infoopenpdf;   openpdf.start(); } 

first, must save file's full name later. so, must change from:

listbox1.items.add(path.getfilename(file)); 

to:

listbox1.items.add(new listitem(path.getfilename(file), file)); 

then, should send file server client, this:

protected void listbox1_selectedindexchanged(object sender, eventargs e) {     string filename = listbox1.selectedvalue;     byte[] filebytes = system.io.file.readallbytes(filename);      system.web.httpcontext context = system.web.httpcontext.current;     context.response.clear();     context.response.clearheaders();     context.response.clearcontent();     context.response.appendheader("content-length", filebytes.length.tostring());     context.response.contenttype = "application/pdf";     context.response.appendheader("content-disposition", "attachment; filename=" + filename);     context.response.binarywrite(filebytes);     context.applicationinstance.completerequest(); } 

note: don't forget initialize listbox property autopostback setted true.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -