新闻动态
新闻动态
- 地方社区防灌水和注册机攻略
- jquery中append()、prepend()、after()、before()的使用区别详解
- thinkphp6执行流程
- php中include引入一个页面的时候为什么用dirname(__FILE__) ?
- 母版页中对控件ID的处理
- 网站标题对SEO优化的影响
- Wap网站适应手机屏幕宽度方案
- 【SVG】路径<Path>标签详解,一次搞懂所有命令参数
- 一篇很不错的有关ASP.NET Session的分析文章
- 关键词有什么特点?
联系我们
邮箱:
手机:15383239821
asp.net
FileUpload.HasFile 属性
作者:
发布时间:2011-05-02
点击:
获取一个值,该值指示 FileUpload 控件是否包含文件。
HasFile 属性获取一个值,该值指示 FileUpload 控件是否包含要上载的文件。 在对要上载的文件执行操作之前,使用该属性来验证该文件是否存在。 例如,在调用 SaveAs 方法将文件保存到磁盘之前,使用 HasFile 属性来验证文件存在。 如果 HasFile 返回 true,则调用 SaveAs 方法。 如果它返回 false,则向用户显示消息,指示控件不包含文件。
下面的示例演示如何创建执行错误检查的 FileUpload 控件。 在保存文件之前,调用 HasFile 方法来验证该控件是否包含要上载的文件。 此外,还调用 File.Exists 方法来检查路径中是否已存在同名的文件。 如果存在同名文件,则在调用 SaveAs 方法之前为要上载的文件的名称前加上一个下划线字符。 这可以防止现有文件被覆盖。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>FileUpload.SaveAs Method Example</title>
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
// Call a helper method routine to save the file.
SaveFile(FileUpload1.PostedFile);
else
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
void SaveFile(HttpPostedFile file)
{
// Specify the path to save the uploaded file to.
string savePath = "c:\\temp\\uploads\\";
// Get the name of the file to upload.
string fileName = FileUpload1.FileName;
// Create the path and file name to check for duplicates.
string pathToCheck = savePath + fileName;
// Create a temporary file name to use for checking duplicates.
string tempfileName = "";
// Check to see if a file already exists with the
// same name as the file to upload.
if (System.IO.File.Exists(pathToCheck))
{
int counter = 2;
while (System.IO.File.Exists(pathToCheck))
{
// if a file with this name already exists,
// prefix the filename with a number.
tempfileName = counter.ToString() + fileName;
pathToCheck = savePath + tempfileName;
counter ++;
}
fileName = tempfileName;
// Notify the user that the file name was changed.
UploadStatusLabel.Text = "A file with the same name already exists." +
"<br />Your file was saved as " + fileName;
}
else
{
// Notify the user that the file was saved successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the uploaded
// file to the specified directory.
FileUpload1.SaveAs(savePath);
}
</script>
</head>
<body>
<h3>FileUpload.SaveAs Method Example</h3>
<form id="Form1" runat="server">
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server">
</asp:Label>
</form>
</body>
</html>
新闻资讯
-
2010-12-09如何早日走出google沙盒?
-
2023-04-29在VMware中安装CentOS7(图文教程)
-
2013-10-31菜鸟之系统建模经验之谈:"机房收费系统"三层架构
-
2012-08-14未来网站的发展方向3
-
2013-10-30.NET/C#中 System.Text.Encoding 类的一个简单用法
-
2025-07-09Nuxt3 创建项目、启动项目、访问项目
相关案例

