c# - Save As dialog box when downloading PDF file -


i'm trying allow webapp download pdf file save dialog. however, after trying, there wasn't save dialog box appeared when used chrome/firefox. when use ie, given error

enter image description here

could error between browser? have saved / convert pdf file bytes.

protected void btnpdf_click(object sender, eventargs e)     {         //byte array hold our pdf, empty         byte[] bytes;          //instead of filestream we'll use memorystream         using (var ms = new system.io.memorystream())         {              //standard pdf setup, itext doesn't care type of stream we're using             var doc = new itextsharp.text.document();             var writer = itextsharp.text.pdf.pdfwriter.getinstance(doc, ms);             doc.open();             pdfptable table = new pdfptable(1);             table.totalwidth = 585f;             table.lockedwidth = true;              var logo = itextsharp.text.image.getinstance(server.mappath("~/image/logo.jpg"));             doc.add(logo);              var titlefont = fontfactory.getfont("arial", 18, font.bold);             doc.add(new paragraph("officer's profile. officer's police id: " + ddlcase.selectedvalue, titlefont));              var normalfont = fontfactory.getfont(fontfactory.helvetica, 14, font.bold);             var phrase = new phrase();              sqlconnection con = new sqlconnection("data source = localhost; initial catalog = project; integrated security = sspi");              sqlcommand cm = new sqlcommand("select lrp.fullname,  pp.profilepic, pp.rank, lrp.policeid, lrp.nric, lrp.email, lrp.contact, lrp.address,lrp.location, pp.achievement, pp.medal1, pp.medal2, pp.medal3, pp.medal4, pp.medal5 loginregisterpolice lrp, policeprofile pp lrp.policeid = '" + ddlcase.selectedvalue + "' , pp.policeid = lrp.policeid", con);             con.open();             sqldatareader dr;              dr = cm.executereader();              if (dr.read())             {                 phrase.add(new chunk("full name :", normalfont));                 phrase.add(dr[0].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                   phrase.add(new chunk("profile picture :\u00a0", normalfont));                 byte[] bytes1 = (byte[])dr[1];                 itextsharp.text.image image1 = itextsharp.text.image.getinstance(bytes1);                 image1.scaletofit(100f, 250f);                 chunk imagechunk1 = new chunk(image1, 0, 0);                 phrase.add(imagechunk1);                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("rank : ", normalfont));                 byte[] bytes2 = (byte[])dr[2];                 itextsharp.text.image image2 = itextsharp.text.image.getinstance(bytes2);                 image2.scaletofit(40f, 300f);                 chunk imagechunk2 = new chunk(image2, 0, 0);                 phrase.add(imagechunk2);                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("police id :", normalfont));                 phrase.add(dr[3].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("nric :", normalfont));                 phrase.add(dr[4].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("email :", normalfont));                 phrase.add(dr[5].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("contact :", normalfont));                 phrase.add(dr[6].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("address :", normalfont));                 phrase.add(dr[7].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("location :", normalfont));                 phrase.add(dr[8].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                  phrase.add(new chunk("achievement :", normalfont));                 phrase.add(dr[9].tostring());                  phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                 phrase.add(chunk.newline);                    phrase.add(new chunk("medal1", normalfont));                 byte[] bytes3 = (byte[])dr[10];                 itextsharp.text.image image3 = itextsharp.text.image.getinstance(bytes3);                 image3.scaletofit(800f, 800f);                 chunk imagechunk3 = new chunk(image3, 0, 0);                 phrase.add(imagechunk3);                   phrase.add(new chunk("medal2", normalfont));                 byte[] bytes5 = (byte[])dr[11];                 itextsharp.text.image image5 = itextsharp.text.image.getinstance(bytes5);                 image5.scaletofit(800f, 800f);                 chunk imagechunk5 = new chunk(image5, 0, 0);                 phrase.add(imagechunk5);                    phrase.add(new chunk("medal3", normalfont));                 byte[] bytes6 = (byte[])dr[12];                 itextsharp.text.image image6 = itextsharp.text.image.getinstance(bytes6);                 image6.scaletofit(800f, 800f);                 chunk imagechunk6 = new chunk(image6, 0, 0);                 phrase.add(imagechunk6);                  phrase.add(new chunk("medal4", normalfont));                 byte[] bytes7 = (byte[])dr[13];                 itextsharp.text.image image7 = itextsharp.text.image.getinstance(bytes7);                 image7.scaletofit(800f, 800f);                 chunk imagechunk7 = new chunk(image7, 0, 0);                 phrase.add(imagechunk7);                  phrase.add(new chunk("medal5", normalfont));                 byte[] bytes8 = (byte[])dr[14];                 itextsharp.text.image image8 = itextsharp.text.image.getinstance(bytes8);                 image8.scaletofit(800f, 800f);                 chunk imagechunk8 = new chunk(image8, 0, 0);                 phrase.add(imagechunk8);                  table.addcell(phrase);               }              dr.close();             doc.add(table);             doc.close();              //grab raw bytes memorystream             bytes = ms.toarray();         }            //at point itext work done , we're dealing raw asp.net parts          //clear current response buffer         response.clear();         //instead of normal text/html header tell browser we've got pdf         response.contenttype = "application/pdf";         //tell browser want file downloaded (ideally) , give pretty filename         response.addheader("content-disposition", "attachment;filename=mysamplefile.pdf");         //write our bytes stream         response.binarywrite(bytes);         //close stream (otherwise asp.net might continue write stuff on our behalf)         response.end();     }     } 

would appreciate if tell me if did wrong here.

i believe issue contenttype. if change to "application/octet-stream", should resolve problem.

response.contenttype = "application/octet-stream"; 

you can try adding:

response.flush() response.close() 

before response.end(). not of buffered data has been transmitted client before ending response.

if doesn't work, should verify have data send in bytes array.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -