<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="up" %>
无标题页 //Code:
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;
using System.IO;
public partial class up : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_ServerClick(object sender, EventArgs e)
{
//取得字符串:“C:\Documents and Settings\Administrator\Desktop\picture\213.jpg”
string fileName = this.FileUpload1.PostedFile.FileName;
//取得从开始到最后一个“\”得长度:fileName.LastIndexOf("\\")
int length = fileName.Length - fileName.LastIndexOf("\\") - 1;
//截取从fileName.LastIndexOf("\\") + 1位置到length位置的字符串
fileName = fileName.Substring(fileName.LastIndexOf("\\") + 1, length);
//取得字符串:“C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\upload2\”
string path = Server.MapPath("upload2\\");
//判断有没有path的文件,如果没有则创建一个新的
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//取得文件的后缀名:“.jpg ”
string s1 = System.IO.Path.GetExtension(fileName);
//取得Web.Config中
配置的值
string s2=System.Configuration.ConfigurationManager.AppSettings["conn"];
//取得上传的文件的大小,单位为bytes
int filesize = (FileUpload1.PostedFile.ContentLength) / 1024;
if (s1 == ".jpg" || s1 == ".gif")
{
//如果文件大小<设定大小,则上传文件到:C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite1\upload2\213.jpg
if (filesize < Convert.ToInt32(s2))
{
FileUpload1.PostedFile.SaveAs(path + fileName);
}
else
{
Response.Write("");
}
}
else
{
Response.Write("");
}
}
}