打开图像的文件上传对话框时,仅图像文件应出现在文件上传对话框中 [英] on opening a file upload dialouge box for image only image file should appear in file upload dialouge box
本文介绍了打开图像的文件上传对话框时,仅图像文件应出现在文件上传对话框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi
我正在使用文件上传对话框在应用程序中上传图像.我想在文件上传对话框中设置一个过滤器,当用户打开它时,只有图像格式的文件应该可见,而没有其他文件出现.请帮助我
hi
I am using file upload dialogue box for upload image in my application. I want to set a filter on file upload dialogue box that when user open it only image format file should be visible in it no other file should appear. please help me
推荐答案
Hi,
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" />
<script type="text/javascript">
function validateFileUpload(obj){
var fileName = new String();
var fileExtension = new String();
// store the file name into the variable
fileName = obj.value;
// extract and store the file extension into another variable
fileExtension = fileName.substr(fileName.length - 3, 3);
// array of allowed file type extensions
var validFileExtensions = new Array("jpg", "png", "gif");
var flag = false;
// loop over the valid file extensions to compare them with uploaded file
for(var index = 0; index < validFileExtensions.length; index++){
if(fileExtension.toLowerCase() == validFileExtensions[index].toString().toLowerCase()){
flag = true;
}
}
// display the alert message box according to the flag value
if(flag == false){
alert('Files with extension ".' + fileExtension.toUpperCase() + '" are not allowed.\n\nYou can upload the files with following extensions only:\n.jpg\n.png\n.gif\n');
return false;
}
else{
alert('File has valid extension.');
return true;
}
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
FileUpload1.Attributes.Add("onchange", "return validateFileUpload(this);");
btnUpload.Attributes.Add("onclick", "return validateFileUpload(document.getElementById('" + FileUpload1.ClientID + "'));");
}
这篇关于打开图像的文件上传对话框时,仅图像文件应出现在文件上传对话框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文