0

I have a link in a view:

<a href="@ViewBag.ReportHelpLink" target="_blank">@ViewBag.ReportHelpText</a>

where @ViewBag.ReportHelpLink results in a link to an Excel document. Such as

http://www.website.com/SomeDocument.xls

Notice, there is a target="_blank" specified. I added this a while ago to make sure that IE doesn't change the current page to blank when the user clicks this link. However, when done this way, IE opens up a new blank window and a download box for the file on top of it.

Is there a way to pop up the download box in IE without that extra blank window or without the current page changing to blank?

As always, other browsers are just fine. It's only IE that wants to open up a blank page one way or the other.

Dimskiy
  • 5,233
  • 13
  • 47
  • 66

1 Answers1

2

Create an iframe and use that as the target for the download. See Download File Using Javascript/jQuery

function downloadURL(url) {
    var hiddenIFrameID = 'hiddenDownloader',
        iframe = document.getElementById(hiddenIFrameID);
    if (iframe === null) {
        iframe = document.createElement('iframe');
        iframe.id = hiddenIFrameID;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
    }
    iframe.src = url;
};

If you want it in HTML only, just set the target of the link to be an iframe

<a href="https://www.swiftview.com/tech/letterlegal5.doc" target="ifr">Download</a>
<iframe name='ifr'></iframe>
Community
  • 1
  • 1
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Ouch! :) I was thinking about something along these lines. Was actually hoping for something simpler. – Dimskiy Mar 05 '14 at 21:29
  • @Dimskiy What's complicated about that? You mean because it uses JS? You can do it in HTML only, the trick to set the target of the link to the iframe. The JS solution just lets you do it without having to setup the iframe in the HTML. – Ruan Mendes Mar 05 '14 at 21:38
  • No no! Your answer is good and thank you very much for it. I'm just in a situation where I don't want to change any code on this page. But I guess, either way I will have to do something, unless business gives up on this requirement. Thanks again! – Dimskiy Mar 05 '14 at 21:48