Ajax implementation file Download _ajax related

Source: Internet
Author: User
Tags file url

jquery Ajax functions of the return type only XML, text, JSON, HTML and other types, there is no "stream" type, so we want to implement Ajax download, can not use the corresponding Ajax functions to download files. But you can use JS to generate a form, use this form to submit parameters, and return the "stream" type of data. In the implementation process, the page is not refreshed.

1. The return value type using Ajax,ajax is json,text,html,xml type, or it can be said that the sending of Ajax, accept can only be string strings, not flow type, so can not implement file download, strong use will appear response conflict. If you do not want to use Ajax, you can only return a file-related URL that is worthwhile to the build. It then creates an IFRAME in the callback function, sets its SRC value to the file URL, or a processing URL to the file generation stream, which enables the file to be downloaded and the page is not refreshed.

2. Do not use Ajax, through the DOM dynamic operation or create a iframe,form way to achieve, download the file at the same time the implementation page does not refresh, where the SRC can be file address URL to download files directly, can also be a stream processing URL through the response stream output download, form is the stream processing URL through the response stream output download, Dom dynamic operation when the implementation of the file download, and the page does not refresh. To download the progress bar at the same time, you can create a timed task, at a certain time to the background to send requests, through the common objects, such as session, to obtain file download processing progress.

var title=$ ("Input[name= ' Gjzselect ')"). Val ();
var rqtime = $ (". Ui-datepicker-time"). Val ();
var ddd= rqtime.split ("one");
var starttime=ddd[];
var endtime=ddd[]; var form = $ ("<form>"); Define a form forms form.attr (' style ', ' display:none ');
Add query parameters to form forms 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); Placing the form in the Web form.append (input);
Submit the query parameter control to the form 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 ("Export failed!") ");}, Success:function (data) {alert (" Export succeeded! "). 
");
} 
}); --%>

The above is a small set for everyone to share the AJAX implementation of file download related knowledge, I hope to help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.