When a project object storage is downloaded, the file name is inconsistent with the page display, and the customer complains of garbled characters

1. Problem description

In a procurement project, the user will actively upload some videos, files and pictures related to the procurement project during the bid evaluation process; The file name displayed on the system page is inconsistent, and the file name is displayed as a string of characters. For customer experience, it is similar to garbled characters, and the experience is poor.

The media described in the project is stored on FDFS. FastDFS is an open source lightweight distributed file system, especially suitable for online services with medium and small files (4KB < FileSize < 500MB), such as video, audio, picture websites, etc. It is an excellent mass data storage and Solutions to load balancing problems.

2. Problem Analysis

1) The file mentioned in the above question exists on the platform environment FDFS file storage system. After the default file is uploaded, the system will sort the file name with a string of characters with a specific code.
insert image description here
2) When downloading with a browser, click the download button. Open the video link, but the download window will not pop up correctly. The video lesson is accessed online (play online) normally, but the file name is displayed as follows when downloading:
insert image description here

insert image description here
Reason: The file index (FID) returned by the Storage server after the file is uploaded to FastDFS, where the file name is regenerated according to the FastDFS custom rules, not the original file name, FastDFS does not store the original file name, and does not provide recovery The method of the original file name; when the user downloads through the browser, if the url downloaded using the http/https protocol is not processed on the web side, when the user clicks to download, the url at this time is the domain name of Nginx + the FID of the resource + "attname=… ..." spelled URL, when the browser reads the URL, the resource file name will be displayed as FDFS "source name"; for the above problems, what we need to do is how to convert the web service (nginx) into the process of accessing fdfs for attname instead of source file name;

3. Problem solving

After consulting and research and development, a similar situation occurred in a sub-project. By modifying the nginx configuration file and configuring the following parameters, the requested file name can be converted into the value of attname:

}
        location /group1/M00 {
    
    
        alias /usr/local/fastdfs//file/data;
             if ($arg_attname ~ "^(.+)") {
    
    
                add_header Content-Type application/x-download;
                add_header Content-Disposition "attachment;filename=$arg_attname";
             }

insert image description here
Through the above configuration, add the field Content-Disposition "attachment;filename=$arg_attname" to the Http response header. When the user's web request arrives, Nginx will intercept the parameter attname in the url, thereby displaying the "resource friendly name".

4. Verification

The effect is as follows:
insert image description here
Note: In addition, some security software in the environment will limit the access rights of the web directory, resulting in abnormal resource download access. If you encounter this problem, you can refer to the inspection and troubleshooting.

5. Appendix: Fdfs file upload and download process

1) Upload process
insert image description here
Among them, the file name generation rules:

storage will generate a FileID for the file, which is spliced ​​by storage server ip + file creation time + file size + file crc32 + random number. This binary string is then base64 encoded and converted to a printable string. The storage will hash according to the file fileid, route it to a subdirectory, and then store the file in the subdirectory with the fileid as the filename. , file suffix (specified by the client, mainly used to distinguish file types) spliced ​​together.

2) Download

When downloading, similar to uploading, the final resource request is still handled by the tracker. The tracker can parse the file's group, size, creation time and other information from the file name, and then select a storage for the request to serve the read request. , and finally return the file ID to the client, access the specific information of the file, and complete the download according to the located file.

For more information, see FastDFS Distributed File Storage .

Guess you like

Origin blog.csdn.net/ximenjianxue/article/details/123571966