<!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>
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;
}
}
}