我的目标是首先验证上传文件是图像,如果是图像,我将显示它.我从验证码和图像代码中获取代码.
起初我能够显示图像.但是,当我将显示图像代码与验证代码相结合时,我无法同时验证和显示工作.
谁能帮帮我吗?以下是我的代码.提前致谢!:)
我能够解决它.下面是正确的代码.:)
JavaScript的
<SCRIPT type="text/javascript"> function ValidateFileUpload() { var fuData = document.getElementById('fileChooser'); var FileUploadPath = fuData.value; //To check if user upload any file if (FileUploadPath == '') { alert("Please upload an image"); } else { var Extension = FileUploadPath.substring( FileUploadPath.lastIndexOf('.') + 1).toLowerCase(); //The file uploaded is an image if (Extension == "gif" || Extension == "png" || Extension == "bmp" || Extension == "jpeg" || Extension == "jpg") { // To Display if (fuData.files && fuData.files[0]) { var reader = new FileReader(); reader.onload = function(e) { $('#blah').attr('src', e.target.result); } reader.readAsDataURL(fuData.files[0]); } } //The file upload is NOT an image else { alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. "); } } } </SCRIPT>
改变的输入:
<input type="file" name="dataFile" id="fileChooser" onchange="return ValidateFileUpload()" />
要在上传之前显示图像:
<img src="images/noimg.jpg" id="blah">