博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
asp.net web常用控件FileUpload(文件上传控件)
阅读量:6899 次
发布时间:2019-06-27

本文共 1784 字,大约阅读时间需要 5 分钟。

hot3.png

FileUpload控件的主要中能:向指定目录上传文件,该控件包括一个文本框和一个浏览按钮。

常用的属性:FileBytes,FileContent、FileName、HasFile、PostedFile。

常用的方法:核心:SaveAs(String filename),  HasFile 的属性ture 和false。

首先在HTML中添加以下代码:加载基本控件

<body>

</div> 第二个练习
<div>
<asp:FileUpload ID="Fileload2" runat="server" style="z-index : 1;" />
<asp:Button ID="确定" runat="server" BorderStyle ="NotSet" Height ="20px" Width ="85px" />
</div>
<div>
<asp:Label ID="Text1" runat ="server" BorderStyle ="NotSet" Height ="20px" Width="85px"></asp:Label>
<br />
<asp:Label ID="Text2" runat ="server" Height ="20px" Width="85px"></asp:Label>
<br />
<asp:Label ID="Text3" runat ="server" Height ="20px" Width="85px"></asp:Label>
<br />
<asp:Label ID="Text4" runat ="server" Height ="20px" Width="85px"></asp:Label>
</div>

在cs文件中添加

protected void 上传_Click(object sender, EventArgs e)

{
bool fileValid = false;
//如果确认了上传文件,则判断文件类型是否符合要求
if(this.Fileload2.HasFile )
{
//获取上传文件的后缀
String fileExtrension = System.IO.Path.GetExtension(this.Fileload2.FileName).ToLower();
String[] restritExension = { ".gif", ".jpg", ".bmp", "png" };
//判断文件类型是否符合要求
for(int i=0;i<restritExension.Length;i++)
{
if(fileExtrension ==restritExension[i])
{
fileValid = true;
}
}
}
//如果文件类型符合要求,调用SaveAs方法实现上传,并显示相关信息
if(fileValid ==true )
{
try
{
this.image1.ImageUrl = "" + Fileload2.FileName;
this.Fileload2.SaveAs(Server.MapPath("") + Fileload2.FileName);
this.Text1.Text = "文件长传成功";

this.Text2.Text += "<li>" + "源文件路径:" + this.Fileload2.PostedFile.FileName;

this.Text3.Text += "<li>" + "文件大小:" + this.Fileload2.PostedFile.ContentLength + "字节";
this.Text4.Text += "<li>" + "文件类型:" + this.Fileload2.PostedFile.ContentType;
}
catch
{
this.Text1.Text = "文件上传不成功!";
}
finally { }
}
else
{
this.Text1.Text = "只能够上传后缀为Gif,jpg,bmp,png的文件";
}
}

转载于:https://my.oschina.net/dongteng/blog/684453

你可能感兴趣的文章
wordpress无法更新为最新版本
查看>>
爬虫代码编写中会遇到的字符处理的坑
查看>>
SSM-Spring-09:Spring中jdk动态代理
查看>>
我为NET狂官方面试题-数据库篇
查看>>
HBase集群安装
查看>>
Ubuntu终端字体大小设置快捷键
查看>>
[20180625]函数与标量子查询13(补充)
查看>>
Python面试问题整理(附答案)
查看>>
设计模式—装饰模式的C++实现
查看>>
MySQL:Innodb page clean 线程 (二) 解析
查看>>
Android CircularFloatingActionMenu:作为系统级按钮悬浮桌面弹出菜单使用(3)
查看>>
正确配置Kubelet可一定程度防止K8S集群雪崩
查看>>
Content Aware ABR技术
查看>>
Spring系列之Spring框架和SpringAOP集成过程分析(十一)
查看>>
云数据库产品月刊·5月刊
查看>>
50行代码的MVVM,感受闭包的艺术
查看>>
Android第三方开源图片裁剪截取:cropper
查看>>
直播转点播实践
查看>>
基于Java语言构建区块链(二)—— 工作量证明
查看>>
Python黑科技:50行代码运用Python+OpenCV实现人脸追踪
查看>>