js download file by URL

A button on the page, after clicking, triggers a function to request data, returns the URL of pdf/epub, and then downloads the file.

Originally, it was written directly with a, the resource address is placed in href, and the target is set to '_blank', which works well. However, all resources will be directly exposed and inconvenient for statistics, so the above method is used instead.

  1. js constructs a tag, js triggers click. It is possible without _blank, but it will replace the original entire page. Adding _blank chrome will block it, and Safari will have no effect.

  2. Fill in the resource address with iframe, src. But this has no effect on Safari. Moreover, if it is a file such as MP4/pdf, the browser does not download it but directly opens it.

  3. window.open is intercepted

The most desired effect is to click the download button, the original page has no changes or refreshes, and the resources are downloaded directly. The mobile version is downloaded on the new page, and the download is triggered to automatically close the new page.

Solution: Use the first method, js constructs a tag, js triggers click. Add the download attribute to the a tag

jsp page:

1 <div class="controls chzn-select fn-left">
2     <button class="button button-flat-primary button-rounded"     type="button" id="export-self-static">导出
3     </button>
4 </div>
5 
6 <a id="exportInfoForm" style="display: none;" download><li id="ex-li">公式管理</li></a>

js page:

1 $('#export-static').unbind('click').bind('click',function(){
2 exports.exportSelfStatistics();
3 });
4 
5 var url = contextPath+"statistics/self/detail/export?" + new Date().getTime()+param;
6 $('#exportInfoForm').attr("href", Util.appEncodeURL(url));
7 $('#ex-li').trigger("click");

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325589982&siteId=291194637