


Detailed explanation of Ajax implementation of file download function
This time I will bring you a detailed explanation of the Ajax implementation file download function, what are the precautions for Ajax implementation of the file download function, the following is a practical case, let's take a look.
The return types of JQuery's ajax function are only xml, text, json, html and other types, and there is no "stream" type, so if we want to implement ajax download, we cannot use the corresponding ajax function to download files. But you can use js to generate a form, use this form to submit parameters, and return "stream" type data. During the implementation process, the page was not refreshed.
1. Using ajax, the return value type of ajax is json, text, html, xml type, or it can be said that the sending and receiving of ajax can only be stringString, not stream type, so file downloading cannot be achieved, and response conflicts will occur if forced use. If you have to use ajax, you can only get the generated file-related URL by returning the value. Then in the Callback function, create an iframe and set its src value to the file URL, or a processing URL for the file generation stream, so that the file can be downloaded without refreshing the page.
2. Without using ajax, it can be realized through dynamic operation of DOM or creation of iframe and form. The page will not be refreshed while downloading the file. The src of the iframe can be the file address url to download the file directly. It can also be that the stream processing URL is downloaded through the response stream output. The form is that the stream processing URL is downloaded through the response stream output. When the DOM is dynamically operated, the file download is implemented without refreshing the page. To implement progress bar while downloading, you can create a scheduled task, send requests to the background at certain intervals, and obtain the progress of file downloading through public objects, such as session.
var title=$("input[name='gjzSelect']").val(); var rqTime = $(".ui-datepicker-time").val(); var ddd= rqTime.split("一"); var startTime=ddd[]; var endTime=ddd[]; var form = $("<form>"); //定义一个form表单 form.attr('style', 'display:none'); //在form表单中添加查询参数 form.attr('target', ''); form.attr('method', 'post'); form.attr('action', "<%=basePath%>jiankong/madeExcel.do"); var input = $('<input>'); input.attr('type', 'hidden'); input.attr('name', 'startTime'); input.attr('value', startTime); var input = $('<input>'); input.attr('type', 'hidden'); input.attr('name', 'endTime'); input.attr('value', endTime); var input = $('<input>'); input.attr('type', 'hidden'); input.attr('name', 'title'); input.attr('value', title); $('body').append(form); //将表单放置在web中 form.append(input); //将查询参数控件提交到表单上 form.append(input); form.append(input); form.submit(); <%-- $.ajax({ url:'<%=basePath%>jiankong/madeExcel.do', cache:false, data:{ 'startTime':startTime, 'endTime':endTime, 'title' :title }, error:function (e){ alert("导出失败!"); }, success:function (data){ alert("导出成功!"); } }); --%>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Adjustment of ajax execution order in jquery
Detailed explanation of Ajax and $.ajax instances
How jQuery makes ajax request for monitoring page
The above is the detailed content of Detailed explanation of Ajax implementation of file download function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Python provides the following options to open downloaded files: open() function: open the file using the specified path and mode (such as 'r', 'w', 'a'). Requests library: Use its download() method to automatically assign a name and open the file directly. Pathlib library: Use write_bytes() and read_text() methods to write and read file contents.

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions
