javascript - Open "Save As" Dialog Box To Download Image -


i've been checking on place answer 1 no luck.

i want button or link open "save as" dialog box. simple?

i know can open image in new window/tab (as i'm doing now) , use right-click, save as method people using not sharpest knives in box, want make download simple possible.

the code @ moment is:

<button class="downloadbutton" type="submit" onclick="window.open('<%=(rscontent.fields.item("contentimage").value)%>')">download image</button> 

but loads image new window/new tab.

just record, users using windows xp internet explorer 8 can't use download html5 event.

i don't mind if javascript, jquery or classic asp.

thanks in advance help.

pb

update

using mdn code lankymart posted, tried as-is , worked (for open/download of excel document), however, tried changing parts download images , didn't work.

here classic asp code:

<% dim rsimage__imageid rsimage__imageid = "1" if (request.querystring("imageid")  <> "")    rsimage__imageid = request.querystring("imageid")  end if %> <% dim rsimage dim rsimage_cmd dim rsimage_numrows  set rsimage_cmd = server.createobject ("adodb.command") rsimage_cmd.activeconnection = mm_eng_string rsimage_cmd.commandtext = "select contentid, contentimage, displayimage tblcontent contentimage = ?"  rsimage_cmd.prepared = true rsimage_cmd.parameters.append rsimage_cmd.createparameter("param1", 5, 1, -1, rsimage__imageid) ' addouble  set rsimage = rsimage_cmd.execute rsimage_numrows = 0 %> 

and (badly) altered mdn code:

<% 'set content type specific type sending. response.contenttype = "image/jpeg"  const adtypebinary = 1 dim strimagefile  strimagefile = (rsimage.fields.item("contentimage").value) 'this path , name of file on disk.   set objstream = server.createobject("adodb.stream") objstream.open objstream.type = adtypebinary objstream.loadfromfile strimagefile  response.binarywrite objstream.read  objstream.close set objstream = nothing %> 

i call using:

<button class="downloadbutton" type="submit" onclick="window.location.href='image-download.asp?imageid=<%=(rscontent.fields.item("contentid").value)%>';">download image</button> 

the error produces is:

the image “http://localhost:85/admin/english/image-download.…p?imageid=5” cannot displayed because contains errors. 

the page code is:

<html>      <head>         <meta name="viewport" content="width=device-width; height=device-height;"></meta>         <link rel="stylesheet" href="resource://gre/res/imagedocument.css"></link>         <link rel="stylesheet" href="resource://gre/res/toplevelimagedocument.css"></link>         <link rel="stylesheet" href="chrome://global/skin/media/toplevelimagedocument.css"></link>         <title>              image-download.asp (jpeg image)          </title>     </head>     <body>         <img src="http://localhost:85/admin/english/image-download.asp?imageid=5" alt="the image “http://localhost:85/admin/english/image-download.…p?imageid=5” cannot displayed because contains errors." title=""></img>     </body>  </html> 

update - related comments in question

you may find need include

response.addheader("content-length", lenb(yourbinary)) 

to stop browsers failing validate payload. it's important the contenttype matches sent if unsure use

response.content = "application/octet-stream" 

there no way initiate save dialog browser via javascript can fake browser displaying save dialog setting attachment value in content-disposition http header.

the way i've tackled use asp page generate image (via com component, adodb.stream, database blob etc) way can use;

response.addheader("content-disposition", "attachment;filename=myimage.jpg") 

this force image saved rather displayed inline. script can pass 1 querystring value display inline (when viewing image) , 1 force attachment force save dialog (browser behaviour may different).

streaming browser

using adodb.stream object output files browser.

in past i've initiated save dialog using javascript (but there nothing stopping using html anchor tag same thing without javascript);

/*  passing myimageid way of identifying image return  database or file system lookup. */ window.location.href = "/myimage.asp?id=myimageid&display=attachment"; 

because response comes attachment location never changed , save dialog displayed name of file passed content-disposition http header in file name box.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -