前台代码:ASPX <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Album_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="Button" /><br /> <asp:Image ID="Image1" runat="server" Height="100px" /><br /> <asp:Label ID="Label1" runat="server"></asp:Label></div> </form></body></html> 后台代码:CS 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 Album_Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { Image1.Visible = false; } protected void Button1_Click(object sender, EventArgs e) { string filename = FileUpload1.FileName; string size = FileUpload1.PostedFile.ContentLength.ToString(); string[] myfile = filename.Split('.'); string dotname = myfile[myfile.Length - 1].ToString().ToLower(); string type = FileUpload1.PostedFile.ContentType; string type2 = filename.Substring(filename.LastIndexOf(".") + 1); string imgpath = Server.MapPath("~/Upimg") + "//"; string filepath = Server.MapPath("~/Upfile") + "//"; string folder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); if (type2 == "jpg" || type2 == "gif") { Image1.Visible = true; if (!System.IO.Directory.Exists(imgpath + folder)) {//自动生成文件夹 System.IO.Directory.CreateDirectory(imgpath + folder); } Random myrdn = new Random();//产生随机数 //日期,时间,随机数和后缀名 string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname; FileUpload1.SaveAs(imgpath + folder + "//" + newfilename); string wpath = "~//Upimg//" + folder + "//" + newfilename; Image1.ImageUrl = wpath; //FileUpload1.SaveAs(ipath); Label1.Text = "原始文件名" + filename + "<br>存储文件名:" + newfilename + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + type + "<br>文件虚拟路径" + wpath; } else { if (!System.IO.Directory.Exists(filepath + folder)) {//自动生成文件夹 System.IO.Directory.CreateDirectory(filepath + folder); } Random myrdn = new Random();//产生随机数 //日期,时间,随机数和后缀名 string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname; Image1.Visible = false; string wpath = "Upfile//" + folder + newfilename; FileUpload1.SaveAs(filepath + folder + "//" + newfilename); Label1.Text = "原始文件名" + filename + "<br>存储文件名:" + newfilename + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + type + "<br>文件虚拟路径" + wpath; } }}