fileUpload.ascx代码:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="fileUpload.ascx.cs" Inherits="fileUpload" %>
<style type="text/css">
.style3
{
width: 196px;
}
.style4
{
width: 507px;
height: 269px;
}
.style5
{
width: 82%;
}
.style6
{
height: 180px;
}
.style7
{
width: 102px;
text-align: right;
}
</style>
<table align="left" cellpadding="0" cellspacing="0" class="style4"
style="background-image: url('upload.JPG')">
<tr>
<td valign="top">
<table align="center" cellpadding="0" cellspacing="0" class="style5"
style="font-size: small">
<tr>
<td class="style6" colspan="3">
</td>
</tr>
<tr>
<td class="style7">
选择文件:</td>
<td class="style3">
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
<td style="text-align: left">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/WebControlUpload/shangchuan.JPG" onclick="ImageButton1_Click" />
</td>
</tr>
<tr>
<td colspan="3">
&nbsp;</td>
</tr>
</table>
</td>
</tr>
</table>

fileUpload.ascx.cs代码:

public partial class fileUpload : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string serverPath = Server.MapPath("UpLoad"); //获取服务器端目录绝对路径
if (!System.IO.Directory.Exists(serverPath)) //如果不存在该目录
{
System.IO.Directory.CreateDirectory(serverPath); //创建该目录
}
if (FileUpload1.HasFile) //判断是否选择上传的文件
{
string imgtype = FileUpload1.PostedFile.ContentType;//获取客户端发送的文件类型
imgtype = imgtype.Substring(, );
if (imgtype != "image")
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择图片');", true);
}
else
{
int filesize = FileUpload1.PostedFile.ContentLength / / ;//获取上传文件的大小
if (filesize > ) //如果大于8M
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('只允许上传不大于8兆的文件');", true); //弹出提示信息
return;
}
else //否则
{
//使用SaveAs方法将上传的文件存储到服务器中
FileUpload1.SaveAs(serverPath + "\\" + FileUpload1.FileName);
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上传成功');", true);
}
}
}
else //如果没有选择文件
{
//弹出提示信息
Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择文件');", true);
return;
}
}
}

Default.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="fileUpload.ascx" tagname="fileUpload" tagprefix="uc1" %>
<!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 runat="server">
<title>具有文件上传功能的用户控件</title>
<style type="text/css">
#form1
{
width: 639px;
height: 152px;
text-align: right;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<uc1:fileUpload ID="fileUpload1" runat="server" />
</form>
</body>
</html>

最终效果图:

最新文章

  1. $(window).height(),在火狐下面获取的高度并不是可视区域的高度
  2. chartjs执行图表
  3. python 定义函数
  4. Initializing Spring root WebApplicationContext
  5. 利用ftrace跟踪内核static tracepoint——实例writeback event
  6. 安装完Apache和PHP之后访问PHP文件页面提示下载而没有解析 解决办法
  7. WEB网页输入框的默认键盘类型控制
  8. [国嵌笔记][023][ARM寻址方式]
  9. 关于hadoop集群下Datanode和Namenode无法访问的解决方案
  10. ASP.NET Core 实战:使用 NLog 将日志信息记录到 MongoDB
  11. JobScheduler android任务调度处理组件(类似QuartZ)
  12. linux命令(35):diff命令
  13. [py]一些搜集到的问题
  14. [LeetCode] 394. Decode String_Medium tag: stack 666
  15. WPF学习笔记-用Expression Design制作矢量图然后导出为XAML
  16. 一款超炫的jquery图片播放插件[Cloud Carousel]
  17. git commit进行代码检查
  18. 量化分析:把Tushare数据源,规整成PyalgoTrade所需格式
  19. PHP——上传头像(1)
  20. Windows下RabbitMQ安装,部署,配置

热门文章

  1. jquery.SuperSlide.2.1.2--轮播(兼容到IE7 适用于整屏)
  2. Easy UI
  3. putty自动登录
  4. angularjs简述
  5. SqlParameter设定value为0却变成null
  6. F2工作流引擎模型
  7. Web Service平台概述
  8. Android调用相册拍照控件实现系统控件缩放切割图片
  9. poj2253
  10. Android启动模式(三种)