[翻译]给ASP.NET2.0 FileUpload控件添加过滤功能
原文: http://aspalliance.com/1614_Adding_Filter_Action_to_FileUpload_Control_of_ASPNET_20
第二步:添加js代码
第四步:Web.config
<appSettings>
<add key="UploadedPath" value="/"/>
</appSettings>
Value值是上传到的虚拟路径,这里写了根目录
介绍的文章是VB.Net,我改成C#放上来
第一步:添加控件
<asp:Label ID="lblUploadErr" runat="server"></asp:Label>
<asp:FileUpload ID="fileDocument" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClientClick="return CheckForTestFile();" onclick="btnUpload_Click" />
<asp:FileUpload ID="fileDocument" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload"
OnClientClick="return CheckForTestFile();" onclick="btnUpload_Click" />
第二步:添加js代码
<script type="text/javascript">
////Trim the input text
function Trim(input)
{
var lre = /^\s*/;
var rre = /\s*$/;
input = input.replace(lre, "");
input = input.replace(rre, "");
return input;
}
// filter the files before Uploading for text file only
function CheckForTestFile()
{
var file = document.getElementById('<%=fileDocument.ClientID%>');
var fileName=file.value;
//Checking for file browsed or not
if (Trim(fileName) =='' )
{
alert("Please select a file to upload!!!");
file.focus();
return false;
}
//Setting the extension array for diff. type of text files
var extArray = new Array(".txt", ".doc", ".rtf", ".pdf", ".sxw", ".odt",
".stw", ".html", ".htm", ".sdw", ".vor");
//getting the file name
while (fileName.indexOf("\\") != -1)
fileName = fileName.slice(fileName.indexOf("\\") + 1);
//Getting the file extension
var ext = fileName.slice(fileName.indexOf(".")).toLowerCase();
//matching extension with our given extensions.
for (var i = 0; i < extArray.length; i++)
{
if (extArray[i] == ext)
{
return true;
}
}
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
file.focus();
return false;
}
</script>
第三步:添加服务器端事件////Trim the input text
function Trim(input)
{
var lre = /^\s*/;
var rre = /\s*$/;
input = input.replace(lre, "");
input = input.replace(rre, "");
return input;
}
// filter the files before Uploading for text file only
function CheckForTestFile()
{
var file = document.getElementById('<%=fileDocument.ClientID%>');
var fileName=file.value;
//Checking for file browsed or not
if (Trim(fileName) =='' )
{
alert("Please select a file to upload!!!");
file.focus();
return false;
}
//Setting the extension array for diff. type of text files
var extArray = new Array(".txt", ".doc", ".rtf", ".pdf", ".sxw", ".odt",
".stw", ".html", ".htm", ".sdw", ".vor");
//getting the file name
while (fileName.indexOf("\\") != -1)
fileName = fileName.slice(fileName.indexOf("\\") + 1);
//Getting the file extension
var ext = fileName.slice(fileName.indexOf(".")).toLowerCase();
//matching extension with our given extensions.
for (var i = 0; i < extArray.length; i++)
{
if (extArray[i] == ext)
{
return true;
}
}
alert("Please only upload files that end in types: "
+ (extArray.join(" ")) + "\nPlease select a new "
+ "file to upload and submit again.");
file.focus();
return false;
}
</script>
protected void btnUpload_Click(object sender, EventArgs e)
{
try
{
//获取上载文件文件大小
int intDocFileLength = fileDocument.PostedFile.ContentLength;
//获取客户端上的文件的完全限定名称
string strPostedFileName = fileDocument.PostedFile.FileName;
//如果大于4MB
if (intDocFileLength > 4096000)
{
lblUploadErr.Text = "上传文件大小大于4MB";
return;
}
if (strPostedFileName != String.Empty)
{
//返回指定路径的扩展名,并转成小写
string strExtn = System.IO.Path.GetExtension(strPostedFileName).ToLower();
if ((strExtn == ".txt") || (strExtn == ".doc") || (strExtn == ".rtf") || (strExtn == ".pdf") ||
(strExtn == ".sxw") || (strExtn == ".odt") || (strExtn == ".html") || (strExtn == ".stw") ||
(strExtn == ".htm") || (strExtn == ".sdw") || (strExtn == ".vor"))
{
//返回与服务器上的指定虚拟路径相对应的物理文件路径
string savePath = Server.MapPath(ConfigurationManager.AppSettings["UploadedPath"]) + "\\";
//保存上传的文件内容
fileDocument.PostedFile.SaveAs(savePath + System.IO.Path.GetFileName(strPostedFileName));
lblUploadErr.Text = "document uploaded successfully";
}
else
{
lblUploadErr.Text = "Please upload a valid Document with the extenion in:<br>";
lblUploadErr.Text = ".txt,.doc,.rtf,.pdf,.sxw,.odt,.stw,.html,.htm,.sdw Or .vor";
}
}
else
{
lblUploadErr.Text = "There is no file to Upload";
}
}
catch (System.Exception ex)
{
lblUploadErr.Text = ex.Message;
}
}
{
try
{
//获取上载文件文件大小
int intDocFileLength = fileDocument.PostedFile.ContentLength;
//获取客户端上的文件的完全限定名称
string strPostedFileName = fileDocument.PostedFile.FileName;
//如果大于4MB
if (intDocFileLength > 4096000)
{
lblUploadErr.Text = "上传文件大小大于4MB";
return;
}
if (strPostedFileName != String.Empty)
{
//返回指定路径的扩展名,并转成小写
string strExtn = System.IO.Path.GetExtension(strPostedFileName).ToLower();
if ((strExtn == ".txt") || (strExtn == ".doc") || (strExtn == ".rtf") || (strExtn == ".pdf") ||
(strExtn == ".sxw") || (strExtn == ".odt") || (strExtn == ".html") || (strExtn == ".stw") ||
(strExtn == ".htm") || (strExtn == ".sdw") || (strExtn == ".vor"))
{
//返回与服务器上的指定虚拟路径相对应的物理文件路径
string savePath = Server.MapPath(ConfigurationManager.AppSettings["UploadedPath"]) + "\\";
//保存上传的文件内容
fileDocument.PostedFile.SaveAs(savePath + System.IO.Path.GetFileName(strPostedFileName));
lblUploadErr.Text = "document uploaded successfully";
}
else
{
lblUploadErr.Text = "Please upload a valid Document with the extenion in:<br>";
lblUploadErr.Text = ".txt,.doc,.rtf,.pdf,.sxw,.odt,.stw,.html,.htm,.sdw Or .vor";
}
}
else
{
lblUploadErr.Text = "There is no file to Upload";
}
}
catch (System.Exception ex)
{
lblUploadErr.Text = ex.Message;
}
}
第四步:Web.config
<appSettings>
<add key="UploadedPath" value="/"/>
</appSettings>
Value值是上传到的虚拟路径,这里写了根目录