A node+express project that I recently contacted needs to have an online preview and download function. After a lot of effort, I finally wrote almost the same. Here is a summary:
One: First, preview through url (you may also preview word and xlsx files in the future, this method cannot be used, and you have to find another method)
This time, the interface is not called, but there is a preview button in the operation column of the list. Then the backend returns the url, type, and id of the file to us in each piece of data in the list.
When you click the preview, you need a pop-up box to display, here is the modal box of layui
We get the file url, and when the popup is completed, assign the url to the element in the popup. (In our project, we want to preview N files, so the carousel component used in the pop-up box)
Judge the file type when assigning, if it is a picture: assign to src of img; if it is pdf: assign to data of object
As long as the url is assigned a value, as long as the url can be used, you can preview it without accident (you can debug it yourself if it involves styles)
Two: The second is to use the preview URL to achieve the download function. (Pro-test pictures and pdf are feasible)
Because this url is for previewing, it is not possible to use ordinary a links, iframes, etc.
After getting the path, you can turn it on the front end first, turn it into a file stream, and then use the a link to download
Three: texture
Four: Paste the code (this is the code in my own project, you can just look at the above ideas)