1. Business ScenarioI have been doing development related to file upload and download recently. When it comes to downloading, I use the following method to download //Get or assign a download path let downUrl; //Use the following method to directly download files window.location.href = downUrl; Business problem: If this file does not exist, the page will jump; For example: 1. The file storage server is down 2. Or the file on the file storage server is deleted Abnormal access as above will cause problems with the download function, and page jumps are unfriendly to users. Here, if we know whether the file exists when downloading, we can solve this problem well. 2. SolutionProvide two solutions 1. Backend solution: Generally, files are stored in a file storage server with a dedicated key. See if there is a separate interface to query whether the file exists, that is, before downloading, query whether the file exists based on the unique key of the file. If it exists, execute the download statement. If it does not exist, give the user a corresponding prompt if(){ //If the file exists, download it }else{ //Otherwise give the corresponding prompt} 2. Front-end solution: The front-end method determines whether the file stream exists I give you the method writing method in vue for practical reference /** * Determine whether the service file exists* @param filepath file address* @param filename * @returns {Boolean} */ isExistFile(filepath, filename){ if(filepath == null || filename == null || filepath === "" || filename ===""){ return false } var xmlhttp; if (window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET",filepath,false); xmlhttp.send(); if(xmlhttp.readyState === 4){ if(xmlhttp.status === 200) return true; //url exists else if(xmlhttp.status === 404) return false; //url does not exist else return false;//other status} } Subsequent logic can be supplemented by yourself. As above, you can test whether the file stream exists. If it exists, we download it. If it does not exist, we give a corresponding prompt. This solves the problem of jumping to a blank page when the file path does not exist. Supplement: "Stream" is an abstract concept, which is an abstract understanding of input and output devices. In Java, data input and output operations are performed in a "stream" manner. Summarize:When we encounter business problems, we can think from both the front-end and back-end perspectives, learn and share to gain new knowledge, and hope to make more progress... This is the end of this article about JavaScript to determine whether a file exists. For more relevant JavaScript to determine whether a file exists, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to recompile Nginx and add modules
>>: MySQL 5.7.21 decompression version installation and configuration method graphic tutorial
# Installation daemon configuration for Redis on ...
I just saw a post titled "Flow Theory and Des...
When you first start learning Linux, you first ne...
This article records the installation and configu...
Check what is installed in mysql rpm -qa | grep -...
Pseudo-arrays and arrays In JavaScript, except fo...
Application software generally has such business ...
<br />User experience is increasingly valued...
The description of echo in the Linux help documen...
When you learn MySQL, you will find that it comes...
Preface I recently wanted to learn CocosCreator, ...
Inline format <colgroup>...</colgroup>...
1. What is Vue Vue is a progressive framework for...
1. Property List Copy code The code is as follows:...
In cells, light border colors can be defined indi...