您的位置:首页 > 编程语言 > ASP

ASp.net C#文件上传

2007-12-11 09:12 447 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>

<!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="Button" /><br />
<asp:Image ID="Image1" runat="server" /><br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.IO;
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 Default7 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string name = FileUpload1.FileName;
string size = FileUpload1.PostedFile.ContentLength.ToString();
string type = FileUpload1.PostedFile.ContentType;
string type2 = name.Substring(name.LastIndexOf(".") + 1);
string ipath = Server.MapPath("upimg") + "//" + name;
string fpath = Server.MapPath("Upfile") + "//" + name;
string wpath = "upimg//" + name;
if (type2 == "jpg" || type2 == "gif")
{
Image1.Visible = false;
if (!Directory.Exists(Server.MapPath("upimg") + "//"))
{
Directory.CreateDirectory(Server.MapPath("upimg") + "//");
}
FileUpload1.SaveAs(ipath);
Image1.ImageUrl = wpath;
Label1.Text = "文件名" + name + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + ipath + "<br>文件虚拟路径" + wpath;
}
else
{
Image1.Visible=false;
if (!Directory.Exists(Server.MapPath("Upfile") + "//"))
{
Directory.CreateDirectory(Server.MapPath("Upfile") + "//");
}
FileUpload1.SaveAs(fpath);
Label1.Text = "文件名" + name + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + ipath + "<br>文件虚拟路径" + wpath;
}
}
}

=====本人学习代码,源代码来自网上
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐