簡體   English   中英

上載PDF文件並驗證大小不超過2mb

[英]Uploading PDF file and validates the size not more than 2mb

任何樣本代碼人員都只能上傳僅能驗證大小不超過2mb的PDF文件。 如果文件大於2mb,系統將不接受該文件或僅刪除表單上的文件,因此用戶將再次上傳正確的文件(小於或等於2mb)。 十分感謝大家。 對不起,我的英語不好。

您可以在HTML文件中嘗試使用此JavaScript代碼。

function SubmitForm() {
                    var imgpath = document.getElementById("fileUpload").value;
                    if(imgpath=="")
                    {
                        document.getElementById("lblError").innerHTML = "No file was chosen before clicking on Upload button. Please chose a file first.";
                        return;
                    }

                    var allowedFiles = [".pdf"];
                    var fileUpload = document.getElementById("fileUpload");
                    var lblError = document.getElementById("lblError");
                    var regex = new RegExp("([a-zA-Z0-9\s_\\.\-:])+(" + allowedFiles.join('|') + ")$");
                    if (!regex.test(fileUpload.value.toLowerCase())) {
                        lblError.innerHTML = "Please upload files having extensions: <b>" + allowedFiles.join(', ') + "</b> only.";
                        return;
                    }
                    if (fileUpload.files[0].size > 2097152){
                        lblError.innerHTML = "File size is more than 2 MB.";
                        return;
                    }
                    lblError.innerHTML = "";
                    return;

                    lblError.innerHTML = "File Upload in Progress.......";
                    document.form.action = "upload_filedata.asp";
                    document.form.submit();

                } 

並將其"JavaScript:SubmitForm();" 與您的“ Submit按鈕。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM