使用Javascript / jQuery下载文件
本文翻译自:Download File Using Javascript/jQuery
I have a very similar requirement specified here . 我在这里指定了非常相似的要求。
I need to have the user's browser start a download manually when $('a#someID').click();
我需要让用户的浏览器在$('a#someID').click();
时手动开始下载$('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. 但我无法使用window.href
方法,因为它将当前页面内容替换为您尝试下载的文件。
Instead I want to open the download in new window/tab. 相反,我想在新窗口/选项卡中打开下载。 How is this possible? 这怎么可能?
#1楼
参考:https://stackoom.com/question/FjLT/使用Javascript-jQuery下载文件
#2楼
If you are already using jQuery, you could take adventage of it to produce a smaller snippet 如果您已经在使用jQuery,那么您可以利用jQuery来生成更小的代码片段
A jQuery version of Andrew's answer: 安德鲁的答案的jQuery版本:
var $idown; // Keep it outside of the function, so it's initialized once.
downloadURL : function(url) {if ($idown) {$idown.attr('src',url);} else {$idown = $('<iframe>', { id:'idown', src:url }).hide().appendTo('body');}
},
//... How to use it:
downloadURL('http://whatever.com/file.pdf');
#3楼
Using anchor tag and PHP it can be done, Check this answer 使用锚标记和PHP可以完成,检查这个答案
JQuery Ajax call for PDF file download JQuery Ajax调用PDF文件下载
HTML<a href="www.example.com/download_file.php?file_source=example.pdf">Download pdf here</a>PHP
<?php
$fullPath = $_GET['fileSource'];
if($fullPath) {$fsize = filesize($fullPath);$path_parts = pathinfo($fullPath);$ext = strtolower($path_parts["extension"]);switch ($ext) {case "pdf":header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a downloadheader("Content-type: application/pdf"); // add here more headers for diff. extensionsbreak;default;header("Content-type: application/octet-stream");header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");}if($fsize) {//checking if file size existheader("Content-length: $fsize");}readfile($fullPath);exit;
}
?>
I am checking for file size because if you load pdf from CDN cloudfront, you won`t get the size of document which forces the document to download in 0kb, To avoid this i am checking with this condition 我正在检查文件大小,因为如果你从CDN cloudfront加载pdf,你不会得到文件的大小迫使文件在0kb下载,为了避免这种情况我正在检查这个条件
if($fsize) {//checking if file size existheader("Content-length: $fsize");}
#4楼
The answer submitted by hitesh on Dec 30 '13 does in fact work. hitesh在13年12月30日提交的答案确实有效。 It just requires a little adjusting: 它只需要一点调整:
The PHP file can call itself. PHP文件可以调用自己。 In other words, just create a file named saveAs.php, and put this code into it... 换句话说,只需创建一个名为saveAs.php的文件,并将此代码放入其中......
<a href="saveAs.php?file_source=YourDataFile.pdf">Download pdf here</a><?phpif (isset($_GET['file_source'])) {$fullPath = $_GET['file_source'];if($fullPath) {$fsize = filesize($fullPath);$path_parts = pathinfo($fullPath);$ext = strtolower($path_parts["extension"]);switch ($ext) {case "pdf":header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a downloadheader("Content-type: application/pdf"); // add here more headers for diff. extensionsbreak;default;header("Content-type: application/octet-stream");header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");}if($fsize) {//checking if file size existheader("Content-length: $fsize");}readfile($fullPath);exit;}}?>
#5楼
function downloadURI(uri, name)
{var link = document.createElement("a");link.download = name;link.href = uri;link.click();
}
Check if your target browser(s) will run the above snippet smoothly: 检查您的目标浏览器是否会顺利运行上述代码段:
http://caniuse.com/#feat=download http://caniuse.com/#feat=download
#6楼
These functions are used in stacktrace.js : 这些函数在stacktrace.js中使用:
/*** Try XHR methods in order and store XHR factory.** @return <Function> XHR function or equivalent*/
var createXMLHTTPObject = function() {var xmlhttp, XMLHttpFactories = [function() {return new XMLHttpRequest();}, function() {return new ActiveXObject('Msxml2.XMLHTTP');}, function() {return new ActiveXObject('Msxml3.XMLHTTP');}, function() {return new ActiveXObject('Microsoft.XMLHTTP');}];for (var i = 0; i < XMLHttpFactories.length; i++) {try {xmlhttp = XMLHttpFactories[i]();// Use memoization to cache the factorycreateXMLHTTPObject = XMLHttpFactories[i];return xmlhttp;} catch (e) {}}
}/*** @return the text from a given URL*/
function ajax(url) {var req = createXMLHTTPObject();if (req) {try {req.open('GET', url, false);req.send(null);return req.responseText;} catch (e) {}}return '';
}
使用Javascript / jQuery下载文件相关推荐
- 在 JavaScript 如何下载文件
在 Web 开发中,文件下载功能是一个非常常见的功能.在本文中,将介绍在 JavaScript 中如何实现下载文件. 使用 location.href 当需要打开新页面时,在 JavaScript 中 ...
- asp.net mvc + javascript生成下载文件
近期做的是对现有项目进行重构.WEB FROM改成MVC,其实也算是推倒重来了. 里面有一个导出功能,将数据输出成txt文件,供下载.原先的做法是有一个隐藏的iframe,在这个iframe的页面中设 ...
- 封装jQuery下载文件组件
使用jQuery导出文档文件 jQuery添加download组件 jQuery.download = function(url, data, method){if( url && d ...
- php.js 文件下载,使用JavaScript开始下载文件
小编典典 我们这样做:首先添加此脚本. function populateIframe(id,path) { var ifrm = document.getElementById(id); ifrm. ...
- 如何用 JavaScript 下载文件
简介 我们知道,下载文件是一个非常常见的需求,但由于浏览器的安全策略的限制,我们通常只能通过一个额外的页面,访问某个文件的 url 来实现下载功能,但是这种用户体验非常不好. 幸好,HTML 5 里面 ...
- post方式下载文件ie8不支持
post方式下载文件ie8不支持 以前有一篇jQuery下载文件的文章post方式下载文件,最近在进行测试的时候发现ie8这个老伙计还是不好用啊.查找错误找到这个问题进行修改后是这样的. 当然,依然需 ...
- javascript下载文件几种方式,接收后台返回流下载或直接下载文件
目录 1 javascript下载文件7中方式 1.1 window.location.href下载 1.2 window.location下载 1.3 iframe下载 1.4 form表单的形式下 ...
- jQuery.Ajax下载文件
jQuery.Ajax下载文件 下面是一个使用插件源代码的简单用例演示. 演示页面包含了许多其他的'更好的用户界面'示例. $.fileDownload('some/file.pdf').done(f ...
- javascript 检测 header下载文件--插件
原理:下载文件时设置一个cookie,客户端利用js间隔性检测cookie,如果检测到则服务端对下载的文件处理完毕,然后通知客户端 http://johnculviner.com/post/2012/ ...
最新文章
- 知乎高赞怎么自学 python,大概要多久?
- mysql charindex_mysql中替代charindex的函数substring_index、find_in_set | 学步园
- 微信小程序开发系列七:微信小程序的页面跳转
- 产品经理如果有捷径,那可能是多读书
- Web Worker
- 反向传播与梯度消失梯度爆炸
- [Noi2015]寿司晚宴
- latex 生成pdf显示行号
- Oracle的expdp导出、impdp导出命令
- 最佳实践 | 联通数科基于 DolphinScheduler 的二次开发
- MySQL从创建数据库到删库跑路之旅
- 禅与摩托车维修艺术中提到的“刀”
- c语言游泳是怎么钓鱼的,不会游泳的钓友进来看 自制饵料钓鲤鱼
- SSAS 系列01- DAX公式常用公式
- 前端知识点------小米官网精灵图书写(两种方式)
- Windows 7 下安装VB6.0出现错误的解决办法
- 注意力机制基本原理详解及应用
- java中上传视频到FTP,从FTP直接播放
- SQL注入——联合查询注入
- app显示服务器繁忙是什么原因,联动云app服务器繁忙