使用Javascript / jQuery下载文件(Download File Using Javascript/jQuery)

我在这里规定了非常相似的要求。

我需要让用户的浏览器手动启动$('a#someID').click();

但是我不能使用window.href方法,因为它将使用您要下载的文件替换当前页面内容。

相反,我想在新窗口/标签中打开下载。 这怎么可能?

I have a very similar requirement specified here.

I need to have the user's browser start a download manually when $('a#someID').click();

But I cannot use the window.href method, since it replaces the current page contents with the file you're trying to download.

Instead I want to open the download in new window/tab. How is this possible?

最满意答案

使用不可见的<iframe> :

<iframe id="my_iframe" style="display:none;"></iframe> <script> function Download(url) { document.getElementById('my_iframe').src = url; }; </script>

要强制浏览器下载文件,否则将能够呈现(例如HTML或文本文件),您需要服务器将文件的MIME类型设置为无关紧要的值,例如application/x-please-download-me或者用于任意二进制数据的application/octet-stream 。

如果您只想在新选项卡中打开它,唯一的方法是让用户点击链接,将其target属性设置为_blank 。

在jQuery中:

$('a#someID').attr({target: '_blank', href : 'http://localhost/directory/file.pdf'});

只要点击该链接,它将在新的选项卡/窗口中下载该文件。

Use an invisible <iframe>:

<iframe id="my_iframe" style="display:none;"></iframe> <script> function Download(url) { document.getElementById('my_iframe').src = url; }; </script>

To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file's MIME Type to a nonsensical value, such as application/x-please-download-me or alternatively application/octet-stream, which is used for arbitrary binary data.

If you only want to open it in a new tab, the only way to do this is for the user to a click on a link with its target attribute set to _blank.

In jQuery:

$('a#someID').attr({target: '_blank', href : 'http://localhost/directory/file.pdf'});

Whenever that link is clicked, it will download the file in a new tab/window.

使用Javascript / jQuery下载文件(Download File Using Javascript/jQuery)

我在这里规定了非常相似的要求。

我需要让用户的浏览器手动启动$('a#someID').click();

但是我不能使用window.href方法,因为它将使用您要下载的文件替换当前页面内容。

相反,我想在新窗口/标签中打开下载。 这怎么可能?

I have a very similar requirement specified here.

I need to have the user's browser start a download manually when $('a#someID').click();

But I cannot use the window.href method, since it replaces the current page contents with the file you're trying to download.

Instead I want to open the download in new window/tab. How is this possible?

最满意答案

使用不可见的<iframe> :

<iframe id="my_iframe" style="display:none;"></iframe> <script> function Download(url) { document.getElementById('my_iframe').src = url; }; </script>

要强制浏览器下载文件,否则将能够呈现(例如HTML或文本文件),您需要服务器将文件的MIME类型设置为无关紧要的值,例如application/x-please-download-me或者用于任意二进制数据的application/octet-stream 。

如果您只想在新选项卡中打开它,唯一的方法是让用户点击链接,将其target属性设置为_blank 。

在jQuery中:

$('a#someID').attr({target: '_blank', href : 'http://localhost/directory/file.pdf'});

只要点击该链接,它将在新的选项卡/窗口中下载该文件。

Use an invisible <iframe>:

<iframe id="my_iframe" style="display:none;"></iframe> <script> function Download(url) { document.getElementById('my_iframe').src = url; }; </script>

To force the browser to download a file it would otherwise be capable of rendering (such as HTML or text files), you need the server to set the file's MIME Type to a nonsensical value, such as application/x-please-download-me or alternatively application/octet-stream, which is used for arbitrary binary data.

If you only want to open it in a new tab, the only way to do this is for the user to a click on a link with its target attribute set to _blank.

In jQuery:

$('a#someID').attr({target: '_blank', href : 'http://localhost/directory/file.pdf'});

Whenever that link is clicked, it will download the file in a new tab/window.