【问题标题】:how to download a file on disk with specific location using javascript or jquery?如何使用 javascript 或 jquery 下载具有特定位置的磁盘上的文件?
【发布时间】:2016-08-02 06:29:34
【问题描述】:

我想使用 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');

【问题讨论】:

    标签: javascript jquery download


    【解决方案1】:

    使用XMLHttpRequest()Blob的形式从服务器请求文件,使用URL.createObjectURL()<a>元素并将download属性和href属性设置为Blob URL,然后在@上调用.click() 987654332@元素。

    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();
    

    【讨论】:

    • cn 你告诉我怎么做
    • get 方法响应为空且未发生任何事情时无法正常工作
    • @SumitAggarwal “当 get 方法响应为 null 且没有发生任何事情时无法正常工作” console.log(this.response) 记录什么?可以创建一个 plnkrplnkr.co 来演示吗?
    • @SumitAggarwal 是的,您能否在 @ 处包含您用来重现 “当 get 方法响应为空且未发生任何事情时无法正常工作”htmljavascript 987654322@,点击Save,分享保存的plnkr?
    【解决方案2】:

    尝试以下方法:

    当ajax完成时创建一个隐藏链接,带有下载属性,触发该链接上的点击事件

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

    【讨论】:

      【解决方案3】:
      var count=0;
      $('#testdownload').on('click', function(){
      count++;
      $('#log').append('<li>Click triggered ' + count + ' times</li>');
      });
      
      $('#testdownload').get(0).click();
      

      Demo

      【讨论】:

      • 我没有可点击的东西
      • 是的,我得到了这个,所以每次页面刷新它都会下载你的文档。
      • 你想要鼠标事件还是什么?
      • 我不想要任何事件,我已经在该函数之间调用了其他 tak 函数,我需要在不受用户干预的情况下自动将文件下载到桌面
      • 好的,所以使用 $('#testdownload').get(0).click();正如我建议的那样,它会下载您在页面中输入的文件。
      【解决方案4】:

      JS

      function(url){
         window.href(url);
      }
      

      【讨论】:

      • 您的代码正在浏览器中打开文件。我想自动下载到我的磁盘空间,因为没有点击按钮
      • window.open(url) opens on newtab nu widow.href(url) 下载它。
      • 对不起使用window.location.href
      • 我不知道为什么有人对此表示赞同。这是完全错误的:href 不是 window 的属性,location.href 不是函数,分配给location.href 与正常导航到它的作用相同。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      • 2021-06-03
      • 2011-09-27
      • 2020-01-05
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多