Handle file download from ajax post

前端 未结 20 2606
心在旅途
心在旅途 2020-11-21 05:46

I have a javascript app that sends ajax POST requests to a certain URL. Response might be a JSON string or it might be a file (as an attachment). I can easily detect Content

20条回答
  •  广开言路
    2020-11-21 06:11

    Here is how I got this working https://stackoverflow.com/a/27563953/2845977

    $.ajax({
      url: '',
      success: function(data) {
        var blob=new Blob([data]);
        var link=document.createElement('a');
        link.href=window.URL.createObjectURL(blob);
        link.download="";
        link.click();
      }
    });

    Updated answer using download.js

    $.ajax({
      url: '',
      success: download.bind(true, "", "")
    });

提交回复
热议问题