简体   繁体   English

如何使用javascript或jquery在磁盘上的特定位置下载文件?

[英]how to download a file on disk with specific location using javascript or jquery?

i want to download a file automatically using javascript or jquery below is the code which i am using but that code open the file in new tab in browser not download in disk. 我想使用javascript或jquery自动下载文件,以下是我正在使用的代码,但是该代码在浏览器的新选项卡中打开文件而不是在磁盘中下载。

function SaveToDisk(fileUrl, fileName) {
    var hyperlink = document.createElement('a');
    hyperlink.href = fileUrl;
    hyperlink.target = '_blank';
    hyperlink.download = fileName || fileUrl;

    (document.body || document.documentElement).appendChild(hyperlink);
    hyperlink.onclick = function() {
       (document.body || document.documentElement).removeChild(hyperlink);
    };

    var mouseEvent = new MouseEvent('click', {
        view: window,
        bubbles: true,
        cancelable: true
    });

    hyperlink.dispatchEvent(mouseEvent);

    // NEVER use "revoeObjectURL" here
    // you can use it inside "onclick" handler, though.
    // (window.URL || window.webkitURL).revokeObjectURL(hyperlink.href);

    // if you're writing cross-browser function:
    if(!navigator.mozGetUserMedia) { // i.e. if it is NOT Firefox
       window.URL.revokeObjectURL(hyperlink.href);
    }
}


        SaveToDisk('http://example.com/service/getUserImage/339/256', 'image.png');

Use XMLHttpRequest() to request file from server as a Blob , utilize URL.createObjectURL() , <a> element with download attribute and href attribute set to Blob URL , then call .click() on <a> element. 使用XMLHttpRequest()从服务器请求文件作为Blob ,利用URL.createObjectURL() <a>与元素download属性和href属性设置为Blob URL ,然后调用.click()<a>元素。

var request = new XMLHttpRequest();
request.open("GET", "http://example.com/service/getUserImage/339/256");
request.responseType = "blob";
request.onload = function() {
  var a = document.createElement("a");
  a.href = URL.createObjectURL(this.response);
  a.download = this.response.name;
  document.body.appendChild(a);
  a.click();
}
request.send();

try the following: 尝试以下方法:

Create a hidden link when the ajax is complete with a attribute of download,trigger a click event on that link 当ajax完成下载属性后,创建一个隐藏链接,触发该链接上的click事件

$('body').append('<a class="hidden-ajax" download href="'+url+'"></a>')
$('.hidden-ajax').trigger('click');

JS JS

function(url){
   window.href(url);
}
var count=0;
$('#testdownload').on('click', function(){
count++;
$('#log').append('<li>Click triggered ' + count + ' times</li>');
});

$('#testdownload').get(0).click();

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Node.js从项目/特定位置下载文件? - How to download a file from project/specific location using nodejs? 如何使用JavaScript设置chrome扩展的文件下载位置? - How to set the file download location for chrome extension Using JavaScript? "使用 JavaScript\/jQuery 下载文件" - Download File Using JavaScript/jQuery 如何在 JavaScript 或 jQuery 中下载文件 - How to download a file in JavaScript or jQuery 如何使用Webix和Javascript下载具有特定名称的文本文件 - How to download a text file with specific name using Webix and Javascript 使用Javascript通过chrome打开完整文件下载URL时如何指定下载位置 - How to specify download location when open the full file download URL via chrome using Javascript 如何使用HTML和Javascript将文件上传到本地计算机中的特定位置 - How to upload a file to a specific location in the local machine using HTML and Javascript 使用JavaScript / Jquery将文本文件下载/更新创建到本地客户端计算机中的特定位置 - Create text file download/update into a particular location in local client machine using JavaScript/Jquery 如何使用 JavaScript 在 Html 中指定下载位置 - How to specify download location in Html using JavaScript 如何在客户端使用 javascript/JQuery 检查文件下载状态 - How to check file download status using javascript/JQuery at client side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM