This is a very simple solution. Well, it's true.
Why do we do this?
Today when the mobile Web is booming, there are too many applications that require users to upload image files on the mobile Web. Because of this, we have some difficulties that we must overcome:
Low network The upload progress is slow at high speed and the user experience is poor
Under high concurrency, the background processing of larger uploaded files is stressful
Maybe there are more...
When overcoming some of the above difficulties, we can also ask ourselves some questions :
Is it really necessary to save the original images uploaded by users?
How long can users wait?
Maybe there are more...
Combined with some of the above difficulties and questions, combined with our actual cases, We may be able to do this - when the user uploads the image, the image is compressed before it is submitted to the background. After the image file size is reduced, the upload speed will naturally increase. Under the same concurrency, the background processing speed will also be improved, and the user experience will be improved.
Some children may say, why not use the form function of some mainstream CDNs to directly upload files to CDN? Of course, you can choose that plan. I just chose one among many plans to use. Or is this the nature of programmers?
Preparation
It has been said above that "when the user uploads the image, the image will be compressed before it is submitted to the background.", then let's prepare various tools immediately :
localResizeIMG (core, used to compress images on the front end)
Vue.js (process front-end data, display logic)
Bootstrap (should I say more?)
How to do it?
Upload the project After the change, use localResizeIMG for compression
Submit the data to the background in the way you want
When localResizeIMG is called, you can specify the width, height and quality of the compressed image (refer to the document for details). As for how to submit the data to In the background, you can refer to the solution mentioned in the library's wiki, everything is very simple.
Demo address
Repository address of this example
The solution in this article is not the only one, nor is it necessarily the best. If you encounter problems when using related frameworks/libraries, you can go to the corresponding Github repository to view the issue or wiki.