標籤:style blog http color os io 檔案 ar 資料
ASP.NET上傳檔案並記錄到資料庫(2011-07-19 11:02:07)
轉載▼
using System;
using System.Data;
using System.Configuration;
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.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == string.Empty)
{
Response.Write("<script>alert(‘請選擇要上傳的檔案!‘);</script>");
}
else
{
//擷取要上傳的檔案的資訊
string filepath = FileUpload1.PostedFile.FileName;//檔案路徑
string oldfilename =filepath.Substring(filepath.LastIndexOf("\\")+1);//檔案名稱
string FileExtension = Path.GetExtension(oldfilename);//檔案的副檔名
int filelength = (int)oldfilename.Length;//檔案名稱長度
//隨機組建檔案名
Random Rnd = new Random();
int strRnd = Rnd.Next(1, 99);
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() + strRnd.ToString() +FileExtension.ToLower();
//將檔案上傳到以當前日期命名的檔案夾中
string UpLoadName =DateTime.Now.Year.ToString()+"-"+DateTime.Now.Month.ToString()+"-"+DateTime.Now.Day.ToString();
bool FileUploadPathExists =File.Exists(Server.MapPath("UpLoad"+"\\"+UpLoadName));
//指定檔案夾不存在,如果不存在就建立該檔案夾
if (!FileUploadPathExists)
{
Directory.CreateDirectory(Server.MapPath("UpLoad"+"\\"+UpLoadName));
}
//儲存上傳的檔案
string savapath = Server.MapPath("UpLoad" + "\\" +UpLoadName);//儲存路徑
string savapath1 = "UpLoad" + "\\" + UpLoadName +"\\"+newfilename;//儲存路徑
FileUpload1.PostedFile.SaveAs(savapath + "\\" + newfilename);
//串連資料庫字串
string connStr = @"DataSource=.\SQLEXPRESS;AttachDbFilename=D:\Web_Test\WebSite4\App_Data\Database.mdf;IntegratedSecurity=True;User Instance=True";
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string cmdtext = "insert intoT_UpFile(FileName,FileLength,FileType,FilePath) values(‘" +newfilename + "‘,‘" + filelength + "‘,‘" + FileExtension +"‘,‘"+savapath1+"‘)";
SqlCommand cmd = new SqlCommand(cmdtext, conn);
try
{
cmd.ExecuteNonQuery();
System.Text.StringBuilder strMsg = newSystem.Text.StringBuilder();
strMsg.Append("<fontcolor=green>檔案被成功添加到資料庫中,詳細資料如下所示:<br>");
strMsg.Append("上傳的檔案的類型為:" +this.FileUpload1.PostedFile.ContentType.ToString() +"<br>");
strMsg.Append("用戶端檔案的地址為:" + filepath +"<br>");
strMsg.Append("上傳檔案的檔案名稱為:" + newfilename +"<br>");
strMsg.Append("檔案上傳到伺服器的路徑為:" + savapath +"<br>");
strMsg.Append("上傳檔案的副檔名為:" + FileExtension +"<br>");
strMsg.Append("上傳檔案的大小為:" + FileUpload1.PostedFile.ContentLength +"個位元組</font>");
this.Label1.Text = strMsg.ToString();
}
catch (Exception error)
{
Response.Write(error.ToString());
}
finally
{
conn.Close();
}
}
}
}
//資料庫設計
create T_UpFile
(
FileID int identity(100000,1) primary key,
FileName varchar(50),
FileLength int ,
FileType varchar(20),
FilePath varchar(MAX),
)
ASP.NET上傳檔案並記錄到資料庫