各位帮忙拉-------如何实现文件名筛选?

sndy77 2003-08-01 09:46:35
比如说,我只想筛选图片文件,如何做到?
...全文
62 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lihao9806 2003-08-01
  • 打赏
  • 举报
回复
你要设置一下Accept属性为“image/*”就可以了。
给你个例子:
<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
<head>

<script language="C#" runat="server">

void Button1_Click(object Source, EventArgs e)
{

if (Text1.Value == "")
{
Span1.InnerHtml = "Error: you must enter a file name";
return;
}

if (File1.PostedFile != null)
{
try
{
File1.PostedFile.SaveAs("c:\\temp\\"+Text1.Value);
Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" +
Text1.Value + "</b> on the Web server";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving file <b>c:\\temp\\" +
Text1.Value + "</b><br>" + exc.ToString();
}
}
}

</script>

</head>
<body>

<h3>HtmlInputFile Sample</h3>

<form enctype="multipart/form-data" runat="server">

Select File to Upload:
<input id="File1"
type="file"
accept="image/*"
runat="server"/>

<p>
Save as filename (no path):
<input id="Text1"
type="text"
runat="server"/>

<p>
<span id=Span1
style="font: 8pt verdana;"
runat="server" />

<p>
<input type=button
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">

</form>

</body>
</html>
xiajianfeng 2003-08-01
  • 打赏
  • 举报
回复
using System.IO;//need use System.IO namespace
xiajianfeng 2003-08-01
  • 打赏
  • 举报
回复
public bool isImage(string strFileName)
{
if(strFileName!="")
{
strFileName = (System.IO.Path.GetFileName(strFileName)).ToLower();

//define allow image type string[] strExt = new string[5];
strExt[0] = ".gif";
strExt[1] = ".jpg";
strExt[2] = ".jpeg";
strExt[3] = ".jpe";
strExt[4] = ".bmp";


//get extend file name
string strExtensions = Path.GetExtension(strFileName);

//Judge image type if (Array.IndexOf(strExt, strExtensions) == -1 && strFileName!="")
return false;
else
return true;
}
else
{
return true;
}
}
sndy77 2003-08-01
  • 打赏
  • 举报
回复
我是想这样,当上传文件时,点击HTMLINPUTFILE控件的浏览按扭时,弹出对话框时只出现图片格式的扩展名。
kyo1209 2003-08-01
  • 打赏
  • 举报
回复
在前台和后台分别判断文件扩展名,会安全一些,例如用right("xxxxx",4)是否等于.jpg。
sndy77 2003-08-01
  • 打赏
  • 举报
回复
老大,给段代码
cyp503 2003-08-01
  • 打赏
  • 举报
回复
判断文件的扩展名,gif,jpg,png,bmp……
sndy77 2003-08-01
  • 打赏
  • 举报
回复
要在弹出的对话西框中实现,不要提交以后在判断。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧