Ajax implements file download and ajax File Download

Source: Internet
Author: User
Tags file url

Ajax implements file download and ajax File Download

JQuery ajax functions only return the xml, text, json, html, and other types, and do not have the "stream" type. Therefore, we need to implement ajax download, the corresponding ajax function cannot be used for file download. However, you can use js to generate a form, use this form to submit parameters, and return "stream" type data. The page is not refreshed during implementation.

1. when ajax is used, the types of ajax return values are json, text, html, and xml, or ajax sending and receiving can only be string strings, but not stream types. Therefore, file downloading cannot be implemented, response conflicts may occur when the application is strongly used. If ajax is required, the generated File url can only be obtained through the return value. Then, create an iframe In the callback function and set its src value to the File url or a processing url for the file generated stream. In this way, the file can be downloaded without refreshing the page.

2. without ajax, you can use dom to perform dynamic operations or create iframe and form. When downloading an object, the page is not refreshed, the src of iframe can be the file Address url to directly download the file, or the stream processing url to download the file through the response stream output, and the form is the stream processing url to download through the response stream output, when dom is used for dynamic operations, files can be downloaded without refreshing the page. To implement a progress bar while downloading, you can create a scheduled task and send a request to the background at a certain time to get the progress of File Download through common objects, such as sessions.

Var title = $ ("input [name = 'gjzselect']"). val (); var rqTime = $ (". ui-datepicker-time "). val (); var ddd = rqTime. split ("1"); var startTime = ddd []; var endTime = ddd []; var form = $ ("<form> "); // define a form. attr ('style', 'display: none'); // Add the query parameter form to the form. attr ('target', ''); form. attr ('method', 'post'); form. attr ('action', "<% = basePath %> jiankong/madeExcel. do "); var input = $ ('<input>'); input. attr ('type', 'hid' En '); input. attr ('name', 'starttime'); input. attr ('value', startTime); var input = $ ('<input>'); input. attr ('type', 'den den '); input. attr ('name', 'endtime'); input. attr ('value', endTime); var input = $ ('<input>'); input. attr ('type', 'den den '); input. attr ('name', 'title'); input. attr ('value', title); $ ('body '). append (form); // place the form in the web form. append (input); // submit the query parameter control to form. append (input); form. append (input); f Orm. 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 (" exported successfully! ") ;}}); -- %>

The above is a small Editor to share with you the Ajax Implementation of File Download related knowledge, I hope to help you!

Articles you may be interested in:
  • Download the Ajax Hacks chm File and source code
  • JQuery uses the dynamic rendering form function to download ajax files
  • Small Example of JQuery's AJAX Implementation of File Download
  • Download a stream file generated by Ajax (implementation code)
  • Causes of file download failure in Jquery Ajax request and Solutions
  • Compatibility of files downloaded during Ajax requests in FireFox

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.