Why AJAX requests cannot download a file

Source: Internet
Author: User
Tags file url json

cause

The return value type of Ajax is Json,text,html,xml type, or it can be said that Ajax receive type can only be string string, not stream type, so file download cannot be implemented. However, the contents of the file can still be obtained with Ajax, the file will be kept in memory and the file cannot be saved to disk. This is because JavaScript cannot interact with the disk, otherwise this is a serious security problem, JS cannot call the browser's download processing mechanism and program, will be blocked by the browser. To implement a method of downloading a hidden form, the window.open () or window.location.href () created in the form of a submission form iframe,iframe src can be a file address URL to download the file directly

Note To set contenttype,content-disposition when downloading
Response. SetHeader ("Content-disposition", "attachment; Filename=123.doc ");
Response.setcontenttype ("Application/msword");


1. The return value type using Ajax,ajax is json,text,html,xml type, or it can be said that Ajax send, accept can only be string string, cannot stream type, so can not implement file download, strong use will appear response conflict.

If you want to use Ajax, you can only return file-related URLs that are worth generating. The callback function is then created by creating an IFRAME and setting its SRC value to the file URL, or a processing URL to the file generation stream, so that the file download is done and the page is not refreshed.

2. Do not use Ajax, through the DOM dynamic operation or create iframe,form way to achieve, while downloading the file implementation of the page is not refreshed, where the SRC can be a 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 file download, and the page does not refresh.

to implement a progress bar while downloading, you can create a timed task that sends a request to the background at regular intervals, through common objects, such as sessions, to get the processing progress of the file download.

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.