ASP.NET上傳檔案並記錄到資料庫

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   檔案   ar   資料   

ASP.NET上傳檔案並記錄到資料庫(2011-07-19 11:02:07) 轉載
標籤:雜談 分類: asp.net學習

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上傳檔案並記錄到資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.