3

I want to prompt user to save data [it could be anything like:- image, pdf, excel sheet etc] in same window.

I don't want to use HTML5 DOWNLOAD option as this is not valid for all the browsers.

For example:-

I want to prompt user to download google logo, as per below code.

<a href="https://www.google.co.in/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"> Download Google Logo</a>

But it will not download Google icon although it will redirect to that link.

Please suggest!!


As per my requirement, target="_blank" or iframe will not solve my concern. I want user will simply download any file in the same page.

Anand Deep Singh
  • 2,560
  • 3
  • 22
  • 28
  • You can't force the user to download the content of an external link. – Linus Oleander Nov 27 '15 at 06:17
  • This is just an example. Actually, there is requirement as per my project that there is a DOWNLOAD link. On click of that link user will get the concerned things like image, pdf , excel etc. – Anand Deep Singh Nov 27 '15 at 06:19
  • a) _"don't want to use HTML5 DOWNLOAD option"_ , b) _"Actually, there is requirement as per my project that there is a DOWNLOAD link"_ Which is requirement ? – guest271314 Nov 27 '15 at 06:23
  • 1
    Possible duplicate of [easiest way to open a download window without navigating away from the page](http://stackoverflow.com/questions/1066452/easiest-way-to-open-a-download-window-without-navigating-away-from-the-page) – Ashraf Purno Nov 27 '15 at 06:24
  • As per my project, there is a grid which allow user to download content from website. There is a download link on the table ---------------------------------------------------------------- # | Content | Action ---------------------------------------------------------------- 1 | Image | Download --------------------------------------------------------------------------------------- 1 | Excel File | Download Now its opening in new page. I want it should download in same page – Anand Deep Singh Nov 27 '15 at 06:28

2 Answers2

0

Try setting href of a element to base64 data URI of image , with type set to "application/octet-stream" , target set to "_self"

<a href="data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAABUAAAAWCAYAAAAvg9c4AAAABHNCSVQICAgIfAhkiAAAACRJREFUOI1j/M/A8J+ByoCJ2gaOGjpq6Kiho4aOGjpq6GA2FAAxTAIqi7SS4AAAAABJRU5ErkJggg==" target="_self">Download image</a>
guest271314
  • 1
  • 15
  • 104
  • 177
  • but when you run this code it will download unknown file which didn't solve my concern and how I encode path as you are showing? – Anand Deep Singh Nov 27 '15 at 07:02
  • _"but when you run this code it will download unknown file"_ If utilized `download` attribute could suggest file name to user at "Save File" dialog . _"how I encode path as you are showing"_ If requested resource has `Access-Control-Allow-Origin` header could use `XMLHttpRequest` , request file as `Blob` , alternatively , download file , convert to `base64` string , `data URI` , use as string at `a` element `href` attribute – guest271314 Nov 27 '15 at 07:05
0

Use the iFrame method.

<iframe id="download" hidden><\iframe>

<a href="javascript:document.getElementById('download').src = 'file-path'>whatever<\a>

-EDIT-

Using the iframe method will not take you to a different page.

Triforcey
  • 453
  • 5
  • 10