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;
}
}
}
=====本人学习代码,源代码来自网上
<!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;
}
}
}
=====本人学习代码,源代码来自网上
相关文章推荐
- asp.net(c#)上传文件时检测文件类型方法小结
- asp.net(C#)多文件上传(源代码)vs2008
- 用ASP.net(c#)上传各种文件的实例
- asp.net,C#,html控件的File控件实现多文件上传简单实例,vs2010
- ASP.NET(C#)实现一次性上传多文件
- asp.net(C#) 上传Execl文件 并插入数据到数据库实例
- JQuery.Uploadify asp.net(C#)文件,多文件上传插件, Session 获取不到等错误
- [翻译]ASP.NET(C#) 中的文件上传进度条
- [翻译]ASP.NET(C#) 中的文件上传进度条
- Asp.net(C#)多文件上传
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- ASP.NET(C#)实现一次性上传多张图片(多个文件)
- FCK在ASP.NET(C#)中的应用,FCK上传文件按照年月的形式保存
- sql server 关于表中只增标识问题 C# 实现自动化打开和关闭可执行文件(或 关闭停止与系统交互的可执行文件) ajaxfileupload插件上传图片功能,用MVC和aspx做后台各写了一个案例 将小写阿拉伯数字转换成大写的汉字, C# WinForm 中英文实现, 国际化实现的简单方法 ASP.NET Core 2 学习笔记(六)ASP.NET Core 2 学习笔记(三)
- asp.net(c#)上传文件时检测文件类型方法小结
- asp.net(c#)上传文件时检测文件类型方法小结
- 图片文件上传, 生成高质量缩略图 (asp.net c#)
- Asp.net(c#)常用文件操作类封装 移动 复制 删除 上传 下载等
- Asp.net(C#)利用File Field多文件上传
- c#、asp.net 基于ajaxfileupload.js 实现文件异步上传