asp.net上传文件
来源:互联网 发布:淘宝买东西银行卡付款 编辑:程序博客网 时间:2024/06/10 08:59
//运行环境Microsoft Visual Studio 2005
//Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
文件路径:<asp:Label ID="Label1" runat="server" Height="21px" Width="709px"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Width="136px"></asp:Label> <br />
<asp:Image ID="Image1" runat="server" Height="136px" Width="230px" />
<br />
</div>
</form>
</body>
</html>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" /><br />
文件路径:<asp:Label ID="Label1" runat="server" Height="21px" Width="709px"></asp:Label><br />
<asp:Label ID="Label2" runat="server" Width="136px"></asp:Label> <br />
<asp:Image ID="Image1" runat="server" Height="136px" Width="230px" />
<br />
</div>
</form>
</body>
</html>
//Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
Image1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
...{
if (FileUpload1.PostedFile.FileName!=null)
...{
//文件路径
string name1 = FileUpload1.PostedFile.FileName;
// Image1.ImageUrl=name1;
//文件名称
string name2 = System.IO.Path.GetFileName(name1);
//文件大小
int filelongth = FileUpload1.PostedFile.ContentLength;
int tt = name1.LastIndexOf(".");
//文件后缀
string filetype = name1.Substring(tt);
//文件后缀转换成小写字母
filetype= filetype.ToLower();
//检查文件的格式
if (filetype != ".gif" && filetype != ".jpg" && filetype != ".jpeg" && filetype != ".bmp" && filetype != ".png")
...{
Response.Write("文件格式不正确!");
Response.End();
}
else
...{
try
...{
FileUpload1.SaveAs(Server.MapPath("picture") + "/" + FileUpload1.FileName);
Image1.ImageUrl = Server.MapPath("picture") + "/" + FileUpload1.FileName;
Image1.Visible = true;
Label1.Text = name1;
Label2.Text = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
}
catch (Exception ex)
...{
}
}
}
else
...{ Label1.Text = "You have not specified a file."; }
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
...{
protected void Page_Load(object sender, EventArgs e)
...{
Image1.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
...{
if (FileUpload1.PostedFile.FileName!=null)
...{
//文件路径
string name1 = FileUpload1.PostedFile.FileName;
// Image1.ImageUrl=name1;
//文件名称
string name2 = System.IO.Path.GetFileName(name1);
//文件大小
int filelongth = FileUpload1.PostedFile.ContentLength;
int tt = name1.LastIndexOf(".");
//文件后缀
string filetype = name1.Substring(tt);
//文件后缀转换成小写字母
filetype= filetype.ToLower();
//检查文件的格式
if (filetype != ".gif" && filetype != ".jpg" && filetype != ".jpeg" && filetype != ".bmp" && filetype != ".png")
...{
Response.Write("文件格式不正确!");
Response.End();
}
else
...{
try
...{
FileUpload1.SaveAs(Server.MapPath("picture") + "/" + FileUpload1.FileName);
Image1.ImageUrl = Server.MapPath("picture") + "/" + FileUpload1.FileName;
Image1.Visible = true;
Label1.Text = name1;
Label2.Text = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
}
catch (Exception ex)
...{
}
}
}
else
...{ Label1.Text = "You have not specified a file."; }
}
}
- asp.net文件上传
- ASP.Net文件上传
- ASP.Net文件上传
- ASP.NET上传文件
- asp.net文件上传
- ASP.NET上传文件
- Asp.Net 文件上传
- asp.net上传文件
- ASP.NET上传文件
- asp.net上传文件
- asp.net文件上传
- ASP.NET上传文件
- ASP.NET文件上传
- ASP.NET上传文件
- asp.net上传文件
- 文件上传(asp.net)
- ASP.NET文件上传
- ASP.NET 文件上传
- ID卡读取方法(用于区分ID卡读取出来的数据和一般人手录入的数据)
- ERP基础之专业名词解释
- PHP简单演示如何使用模板制作静态页面
- BAT批处理文件语法
- 防止表格被撑开或div层被撑开的多种方法
- asp.net上传文件
- 2008年度IT界最值得关注的商业模式
- 电脑罢工啦!
- 在C#中如何调用dll文件
- 软性方式留住人才,强悍团队的四个要素
- linux的一点小bug
- 化解字符串不能超过8000的方法及交叉表的处理
- ShapeToVml的一个类体
- 分析:新《劳动合同法》对IT业是好是坏