This time I will bring you the method of H5reading filesand uploading them to the server. What are the precautions of the method of H5 reading files and uploading them to the server? The following are Let’s take a look at practical cases.
Note: When uploading using Ajax, the file should not be too large, preferably less than three to four hundred megabytes, because too many consecutive Ajax requests will cause the background to crash and the data in the InputStream will be empty, especially when browsing on Google during device testing.
1. Simply read the file into Blobs in segments and upload it to the server with ajax
1 2 3 4 5 6 7 8 9 |
|
JS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
Backend code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
2. Read the file into blobs in sections, and upload it to the server using ajax, adding the stop and continue function operations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
JS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
|
The background code is the same as above
3. Read the file in segments into a binary array and upload it to the server using ajax
Using binary array transfer is very inefficient, and the final file still deviates somewhat from the original size
HTML content is the same as above
JS:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
Backend code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
I believe you have mastered the method after reading the case in this article, and there will be more exciting things Please pay attention to other related articles on php Chinese website!
Recommended reading:
How to achieve the animation effect of picture rotation in html5
H5 implements desktop notification
The above is the detailed content of How to read files in H5 and upload them to the server. For more information, please follow other related articles on the PHP Chinese website!