Recently, I encountered such a function when I was working on a website. In the page as shown in the figure, when users need to click the link, they can judge whether the corresponding excel file is stored in the server in an asynchronous ajax way. If not, they will be prompted that they have not found it, and if so, they will download it to the local user.
Of course, this is a very simple problem. Just write Ajax in a normal way. But when the server returns the file content to the browser in binary form, the browser’s Ajax throws an error. It’s about parseError, invalid XML, PK, etc.
The reason for this problem is not that there is a problem with the server-side code or JavaScript code, but that downloading files through AJAX is forbidden. For security reasons, JavaScript is not able to save the file to the local, so Ajax takes this into account. It only accepts the return value in XML, Ajax and JSON format, and the binary return format will throw this exception.
How to solve this problem?use window.location =URL is OK. Some people will ask, such as the above figure, when you click the download link on a certain page, because it has changed window.location Is the current page about to jump?In fact, I use Chrome browser. When I click that link, the next file save dialog box will pop up directly, and the address bar of the page has no change. At this time, if you click save, the file will be kept. If you click cacel, the operation will be cancelled. During the process, the current page will be kept and will not jump to other pages.
From personal blog www.sunrobin.net
Read More:
- Google cloud disk: too many users have recently viewed or downloaded this file. Please try to access this file later. (How to Bypass Google Drive Download Limit (Quota Exceeded) Error)
- Ubuntu 20.04 builds nginx 1.18.0 and MAC file transfer / download to server
- In the HTML page request Ajax times 400 error, solve Yii submit post form 400 error, and Ajax post request 400 problem (example code)
- Keil prompts the stlink download program to prompt error: Flash download failed – target DLL has been cancelled
- Transfer: completely solve the problem that the browser home page is tampered by deleting the script file
- To solve the problem of nginx accessing PHP file into download
- About OSS file download and rename
- The SuperMap webgl browser reported an error. The unzip.wasm file 404
- Ajax error reporting cross domain, AJAX cross domain access error 501 solution
- Bug: unable to download source code in idea, error cannot download sources sources not found for:XXX
- Apple computer download file automatically with suffix problem solving
- Mac realizes file interaction (upload and download) between terminal and server
- When to enter success and error in AJAX
- File under WGet download directory
- ASP.NET AJAX client-side framework failed to load
- ajax error 400 (Failed to load resource: the server responded with a status of 400 (Bad Request))
- NPM modify download dependency (modify global download and cache path)
- Browser console prompt: Error: NetworkError when attempting to fetch resource.
- Linux QT download / usr / include / C + + / 7/ cstdlib:75 : 15: fatal error: stdlib. H: no such file or directory error
- How do I download or save a YouTube video to my computer?