Home > Web Front-end > JS Tutorial > body text

How to request download of Execl file via Ajax

不言
Release: 2018-07-21 10:51:39
Original
4928 people have browsed it

This article shares with you how to request to download an Execl file through Ajax. The content is very good. Friends in need can refer to it. I hope it can help everyone.

The problem of downloading Execl through Ajax request fell into a pit for more than half an hour, so I will record it here.

At first I mistakenly thought it was a background problem, but after debugging it was not the case, and no error was reported, and the success function was entered.

The following events and request parameters seem to be fine.

<a href="#" class="easyui-linkbutton" onclick="downLoadExcelTemplate()">下载模板</a>
//下载模板            
function downLoadExcelTemplate() {                  
$.ajax({                        
type: &#39;POST&#39;,                        
url: "DownLoadExcelTemplate",                        
data: null,                        
success: function (redata) {                             
}                  
});            
};
Copy after login

In fact, this is the situation. Downloading files cannot be done directly through Ajax. If you need to use Ajax operation, we can write like this

After the request is successful, we create a < ;a> tag and then add the href attribute, and finally trigger the attribute and it will be OK

function downLoadExcelTemplate() {  
$.ajax({    
type: &#39;POST&#39;,    
url: "DownLoadExcelTemplate",    
data: null,    
success: function (redata) {
      var $a = $("<a");                              
      $a.attr("href", "DownLoadExcelTemplate");                              
      $("body").append($a);                              
      $a[0].click();                              
      $a.remove();
    }  
    });
    };
Copy after login

Related recommendations:

How to dynamically load JS and CSS files and code scripts in native JS

How to implement the method of uploading and compressing images in js

The above is the detailed content of How to request download of Execl file via Ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!