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

时间:2022-06-16 05:10:23

Any sample codes guys about the uploading a PDF file only that validates the size not more than 2mb. If the file is more than 2mb the system will not accept the file or just delete the file on the form so the user will upload again the right file(less than or equal to 2mb). Thank you so much guys. and sorry for my bad english.

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

1 个解决方案

#1


0  

You may try this JavaScript code with your html file.

您可以使用您的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();

                } 

And Call this "JavaScript:SubmitForm();" with you Submit button.

并称之为“JavaScript:SubmitForm();”与你一起提交按钮。

#1


0  

You may try this JavaScript code with your html file.

您可以使用您的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();

                } 

And Call this "JavaScript:SubmitForm();" with you Submit button.

并称之为“JavaScript:SubmitForm();”与你一起提交按钮。