JavaScript example code to determine whether a file exists

JavaScript example code to determine whether a file exists

1. Business Scenario

I 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. Solution

Provide 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:
  • Using javascript to determine if a file exists
  • JavaScript determines whether a file exists instance code
  • Javascript determines whether a file exists (client/server)
  • Code for using fso to determine whether a file exists in JavaScript

<<:  How to recompile Nginx and add modules

>>:  MySQL 5.7.21 decompression version installation and configuration method graphic tutorial

Recommend

How to fix the four sides of the table to scroll up, down, left and right

question: When I was doing project statistics rec...

HTML table tag tutorial (46): table footer tag

The <tfoot> tag is used to define the style...

Analysis of Vue element background authentication process

Preface: Recently, I encountered a management sys...

Ten important questions for learning the basics of Javascript

Table of contents 1. What is Javascript? 2. What ...

Example code for using @media in CSS3 to achieve web page adaptation

Nowadays, the screen resolution of computer monit...

Reasons why MySQL queries are slow

Table of contents 1. Where is the slowness? 2. Ha...

The difference between MySQL count(1), count(*), and count(field)

Table of contents 1. First look at COUNT 2. The d...

How to call a piece of HTML code together on multiple HTML pages

Method 1: Use script method: Create a common head...

What does this.parentNode.parentNode (parent node of parent node) mean?

The parent node of the parent node, for example, t...

4 solutions to CSS browser compatibility issues

Front-end is a tough job, not only because techno...

WeChat applet realizes taking photos and selecting pictures from albums

This article shares the specific code for WeChat ...

MySQL master-slave synchronization principle and application

Table of contents 1. Master-slave synchronization...

Vue state management: using Pinia instead of Vuex

Table of contents 1. What is Pinia? 2. Pinia is e...

Dockerfile implementation code when starting two processes in a docker container

I want to make a docker for cron scheduled tasks ...