using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Admin_UploadFile : System.Web.UI.UserControl
{
private string uppaths = "../uploads";//必须存在
private int uptype = 2;
private int uplimite = 100;
private string filetype = "jpg|gif|png|bmp";
private string cssclass = "";
private string filepath = "";
// 上传路径
public string upPaths
{
get { return uppaths; }
set { uppaths = value; }
}
//上传类型(1:原文件名 2:日期时间)
public string upType
{
get { return uptype.ToString(); }
set { uptype = Convert.ToInt32(value); }
}
//限制大小(KB)
public string upLimite
{
get { return uplimite.ToString(); }
set { uplimite = Convert.ToInt32(value);}
}
//可上传文件类型(扩展名)
public string fileType
{
get { return filetype; }
set { filetype = value; }
}
public string CssClass
{
get { return cssclass; }
set { cssclass = value; }
}
public string filePath
{
get { return PicPath.Text; }
set
{
filepath = value;
PicPath.Text = filepath;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UploadBox.CssClass = cssclass;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string fullname;
fullname = FileUpload1.FileName.ToString();
string url = FileUpload1.PostedFile.FileName.ToString();//这个是以前2003用的,先取得全部的上传文件路径个名字,然后再利用SubString方法来得到用户名,现在看来是没有必要了
string typ = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
long size = FileUpload1.PostedFile.ContentLength / 1024;
int i = 0;
if (uptype != 1)
{
fullname = System.DateTime.Now.ToString("yyyyMMddhhmmss") + "." + typ2;
}
string[] sArray = filetype.Split('|');
foreach (string s in sArray)
{
if (s == typ2)
{
i = i + 1;
}
}
if (i > 0)
{
if (uplimite >= size)
{
FileUpload1.SaveAs(Server.MapPath(uppaths) + "\\" + fullname);//将文件保存在跟目录的UP文件夹下
PicPath.Text = uppaths + @"/" + fullname;
Utility.jsUtility.Alert("上传成功!");
}
else
{
Utility.jsUtility.Alert("您上传的文件不能超过 " + uplimite.ToString() + " KB!");
}
}
else
{
Utility.jsUtility.Alert("文件类型错误!支持类型:" + filetype);
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
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 Admin_UploadFile : System.Web.UI.UserControl
{
private string uppaths = "../uploads";//必须存在
private int uptype = 2;
private int uplimite = 100;
private string filetype = "jpg|gif|png|bmp";
private string cssclass = "";
private string filepath = "";
// 上传路径
public string upPaths
{
get { return uppaths; }
set { uppaths = value; }
}
//上传类型(1:原文件名 2:日期时间)
public string upType
{
get { return uptype.ToString(); }
set { uptype = Convert.ToInt32(value); }
}
//限制大小(KB)
public string upLimite
{
get { return uplimite.ToString(); }
set { uplimite = Convert.ToInt32(value);}
}
//可上传文件类型(扩展名)
public string fileType
{
get { return filetype; }
set { filetype = value; }
}
public string CssClass
{
get { return cssclass; }
set { cssclass = value; }
}
public string filePath
{
get { return PicPath.Text; }
set
{
filepath = value;
PicPath.Text = filepath;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UploadBox.CssClass = cssclass;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string fullname;
fullname = FileUpload1.FileName.ToString();
string url = FileUpload1.PostedFile.FileName.ToString();//这个是以前2003用的,先取得全部的上传文件路径个名字,然后再利用SubString方法来得到用户名,现在看来是没有必要了
string typ = FileUpload1.PostedFile.ContentType.ToString();//获取文件MIME内容类型
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//获取文件名字 . 后面的字符作为文件类型
long size = FileUpload1.PostedFile.ContentLength / 1024;
int i = 0;
if (uptype != 1)
{
fullname = System.DateTime.Now.ToString("yyyyMMddhhmmss") + "." + typ2;
}
string[] sArray = filetype.Split('|');
foreach (string s in sArray)
{
if (s == typ2)
{
i = i + 1;
}
}
if (i > 0)
{
if (uplimite >= size)
{
FileUpload1.SaveAs(Server.MapPath(uppaths) + "\\" + fullname);//将文件保存在跟目录的UP文件夹下
PicPath.Text = uppaths + @"/" + fullname;
Utility.jsUtility.Alert("上传成功!");
}
else
{
Utility.jsUtility.Alert("您上传的文件不能超过 " + uplimite.ToString() + " KB!");
}
}
else
{
Utility.jsUtility.Alert("文件类型错误!支持类型:" + filetype);
}
}
}
界面
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UploadFile.ascx.cs" Inherits="Admin_UploadFile" %>
<asp:Panel ID="UploadBox" runat="server">
上传图片:<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="开始上传" OnClick="Button1_Click" />
<br />
图片路径:<asp:TextBox ID="PicPath" runat="server" Width="280px" />
</asp:Panel>
<asp:Panel ID="UploadBox" runat="server">
上传图片:<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="开始上传" OnClick="Button1_Click" />
<br />
图片路径:<asp:TextBox ID="PicPath" runat="server" Width="280px" />
</asp:Panel>