上传PDF文件并验证大小不超过2mb

时间:2016-08-18 02:48:43

标签: javascript html asp-classic

有关上传PDF文件的任何示例代码都会验证大小不超过2mb。如果文件大于2mb,系统将不接受该文件或只删除表单上的文件,以便用户再次上传正确的文件(小于或等于2mb)。十分感谢大家。抱歉我的英语不好。

1 个答案:

答案 0 :(得分:0)

您可以在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