如何在UploadFile Control中过滤文件? [英] How to filter files in UploadFile Control ?
问题描述
在我的应用中,我有一个下拉菜单和文件上传控件.
在下拉菜单中,我有以下项目:
Hi,
In my app I have a dropdown and file upload control.
In the dropdown I have the following items:
<asp:ListItem Value="1">Text</asp:ListItem>
<asp:ListItem Value="2">Word</asp:ListItem>
<asp:ListItem Value="3">Excel</asp:ListItem>
现在,我已经上传了一个文件,在上面的下拉列表中已经选择了文件类型,我的要求是当我在下拉列表中选择文本"时.我只希望在浏览"按钮的窗口("Modep弹出窗口")中向用户显示.txt文件.
我该如何解决?如果知道,请帮帮我.
谢谢&问候
Honey
Now, I have uploaded a file, the type of a file is already selected in the above drop down, my requrient is when I selected "Text" in dropdown. I want to show only .txt files to user in the window(Modep Pop Up Window) of Browse button.
How I can solve this? If you know help me.
Thanks & Regards
Honey
推荐答案
本文应为您提供帮助-将筛选器操作添加到ASP的FileUpload控件中.NET 2.0 [ ^ ]:竖起大拇指:
This article should help you - Adding Filter Action to FileUpload Control of ASP.NET 2.0[^] :thumbsup:
默认情况下,FileUpload控件不支持您的需求.
但是,当您选择文件时,可以使用javascript检查文件扩展名.
By default the FileUpload control doesn''t support your demand.
But, when you select the file, you can check the file extension using javascript.
<script type="text/javascript">
function validateFileUpload(obj,exten){
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(exten);
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:".' + exten + '"');
return false;
}
else{
alert('File has valid extension.');
return true;
}
}
</script>
关于Page_Load事件
on Page_Load event
FileUpload1.Attributes.Add("onchange", "return validateFileUpload(this,''" + DropDownList1.SelectedItem.Text + "'');");
如果您需要有关此方面的进一步帮助,请访问此处
或
通过使用RegularExpressionValidator,您可以基于文件的扩展名来限制文件类型.
If you need the further help regarding this please visit here
OR
By using a RegularExpressionValidator you can limit the file type based on it''s extension.
这篇关于如何在UploadFile Control中过滤文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!