|
| 1 | +--- |
| 2 | +title: Client-Side file size validation |
| 3 | +description: Client-Side file size validation. Check it now! |
| 4 | +type: how-to |
| 5 | +page_title: Client-Side file size validation. | RadAsyncUpload |
| 6 | +slug: asyncupload-client-side-file-size-validation |
| 7 | +res_type: kb |
| 8 | +tags: asyncupload, validation, client side validation, async upload client side validation, telerik, telerik ajax |
| 9 | +--- |
| 10 | + |
| 11 | +## Environment |
| 12 | + |
| 13 | +<table> |
| 14 | + <tbody> |
| 15 | + <tr> |
| 16 | + <td>Product</td> |
| 17 | + <td>Telerik WebForms AsyncUpload for ASP.NET AJAX</td> |
| 18 | + </tr> |
| 19 | + </tbody> |
| 20 | +</table> |
| 21 | + |
| 22 | +## Description |
| 23 | + |
| 24 | +The aim of the article is to gain some kind of client side validation to restrict users from adding more than a pre-determined limit of files based upon total size of all uploaded files, not just each file or number of files. |
| 25 | + |
| 26 | +## Solution |
| 27 | + |
| 28 | +This JavaScript code enforces client-side validation to prevent users from uploading more files than a preset limit based on the total size of all files uploaded, handling duplicate file detection and exceeding total size limits while providing real-time feedback during the upload process. |
| 29 | + |
| 30 | +````ASP.NET |
| 31 | +<table> |
| 32 | + <tr> |
| 33 | + <td colspan="2">You may attach up to 20mb in files to the Email.</td> |
| 34 | + </tr> |
| 35 | + <tr> |
| 36 | + <td>Select File(s):</td> |
| 37 | + <td> |
| 38 | + <telerik:RadAsyncUpload ID="RadAsyncUpload1" |
| 39 | + OnClientFileSelected="OnFileSelected" |
| 40 | + OnClientFileUploadFailed="OnFileUploadFailed" |
| 41 | + OnClientFilesUploaded="OnFilesUploaded" |
| 42 | + OnClientProgressUpdating="OnProgressUpdating" |
| 43 | + OnClientFileUploaded="OnFileUploaded" |
| 44 | + OnClientFileUploadRemoved="OnFileUploadRemoved" |
| 45 | + runat="server"> |
| 46 | + </telerik:RadAsyncUpload> |
| 47 | + </td> |
| 48 | + </tr> |
| 49 | +</table> |
| 50 | +```` |
| 51 | + |
| 52 | +````JavaScript |
| 53 | +var uploadsInProgress = 0; |
| 54 | +var MAX_TOTAL_BYTES = 20971520; |
| 55 | +var filesSize = new Array(); |
| 56 | +var OVERSIZE_MESSAGE = "You are only allowed to add up to 20mb of files total"; |
| 57 | +var isDuplicateFile = false; |
| 58 | + |
| 59 | +function OnFileSelected(sender, args) { |
| 60 | + for (var fileindex in sender._uploadedFiles) { |
| 61 | + if (sender._uploadedFiles[fileindex].fileInfo.FileName == args.get_fileName()) { |
| 62 | + isDuplicateFile = true; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (!uploadsInProgress) { |
| 67 | + $("input[id$=btnSave]").attr("disabled", "disabled"); |
| 68 | + } |
| 69 | + uploadsInProgress++; |
| 70 | +} |
| 71 | + |
| 72 | +function OnFilesUploaded(sender, args) { |
| 73 | + if (sender._uploadedFiles.length == 0) { |
| 74 | + filesSize = new Array(); |
| 75 | + uploadsInProgress = 0; |
| 76 | + $("input[id$=btnSave]").removeAttr("disabled"); |
| 77 | + } |
| 78 | + |
| 79 | + if (uploadsInProgress > 0) { |
| 80 | + DecrementUploadsInProgress(); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +function OnProgressUpdating(sender, args) { |
| 85 | + filesSize[args.get_data().fileName] = args.get_data().fileSize; |
| 86 | +} |
| 87 | + |
| 88 | +function OnFileUploadFailed(sender, args) { |
| 89 | + DecrementUploadsInProgress(); |
| 90 | +} |
| 91 | + |
| 92 | +function OnFileUploaded(sender, args) { |
| 93 | + DecrementUploadsInProgress(); |
| 94 | + var totalBytes = 0; |
| 95 | + var numberOfFiles = sender._uploadedFiles.length; |
| 96 | + |
| 97 | + if (isDuplicateFile) { |
| 98 | + sender.deleteFileInputAt(numberOfFiles - 1); |
| 99 | + isDuplicateFile = false; |
| 100 | + sender.updateClientState(); |
| 101 | + alert("You can't add the same file twice"); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + for (var index in filesSize) { |
| 106 | + totalBytes += filesSize[index]; |
| 107 | + } |
| 108 | + |
| 109 | + if (totalBytes > MAX_TOTAL_BYTES) { |
| 110 | + sender.deleteFileInputAt(numberOfFiles - 1); |
| 111 | + sender.updateClientState(); |
| 112 | + alert(OVERSIZE_MESSAGE); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +function OnFileUploadRemoved(sender, args) { |
| 117 | + if (args.get_fileName() != null) { |
| 118 | + if (!isDuplicateFile) { |
| 119 | + delete filesSize[args.get_fileName()]; |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +function DecrementUploadsInProgress() { |
| 125 | + uploadsInProgress--; |
| 126 | + if (!uploadsInProgress) { |
| 127 | + $("input[id$=btnSave]").removeAttr("disabled"); |
| 128 | + } |
| 129 | +} |
| 130 | +```` |
| 131 | + |
| 132 | + |
0 commit comments