<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <script runat="server"> void btnUpload_OnClick(Object sender, EventArgs e) { this.pnlUpload.Visible = false; this.pnlSave.Visible = true; if(!this.fiUpload.HasFile) { this.lblText.Text = "Please select upload file!"; } else { this.fiUpload.SaveAs(Server.MapPath("Myfiles/"+fiUpload.FileName)); this.lblText.Text = "<b>" + fiUpload.FileName + "</b> Uploaded.<br>"; //*** Save to Database ***// OleDbConnection objConn; OleDbCommand objCmd; String strConnString,strSQL; strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb") + ";"; objConn = new OleDbConnection(strConnString); objConn.Open(); strSQL = "INSERT INTO picture (PictureName) " + "VALUES ('" + fiUpload.FileName + "')"; objCmd = new OleDbCommand(); objCmd.Connection = objConn; objCmd.CommandType = CommandType.Text; objCmd.CommandText = strSQL; objCmd.ExecuteNonQuery(); objCmd = null; objConn.Close(); objConn = null; this.hplLink1.NavigateUrl = "AspNetFileUploadToDatabase1.aspx"; this.hplLink2.NavigateUrl = "AspNetFileUploadToDatabase2.aspx"; } } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - Upload files</title> </head> <body> <form id="form1" runat="server"> <asp:Panel id="pnlUpload" runat="server"> <asp:FileUpload id="fiUpload" runat="server"></asp:FileUpload> <input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick" value="Upload" runat="server" /> </asp:Panel> <asp:Panel id="pnlSave" Visible="false" runat="server"> <asp:Label id="lblText" runat="server"></asp:Label><br /> <asp:HyperLink id="hplLink1" runat="server">Upload again</asp:HyperLink> , <asp:HyperLink id="hplLink2" runat="server">View files</asp:HyperLink> </asp:Panel> </form> </body> </html>
<%@ Page Language="C#" Debug="true" %> <%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <script runat="server"> OleDbConnection objConn; OleDbCommand objCmd; void Page_Load(Object sender, EventArgs e) { String strConnString; strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("database/mydatabase.mdb") + ";"; objConn = new OleDbConnection(strConnString); objConn.Open(); BindData(); } void BindData() { String strSQL; strSQL = "SELECT * FROM picture"; OleDbDataReader dtReader; objCmd = new OleDbCommand(strSQL, objConn); dtReader = objCmd.ExecuteReader(); //*** BindData to Repeater ***// myRepeater.DataSource = dtReader; myRepeater.DataBind(); dtReader.Close(); dtReader = null; } void Page_UnLoad() { objConn.Close(); objConn = null; } </script> <html> <head> <title>ThaiCreate.Com ASP.NET - Upload files</title> </head> <body> <form id="form1" runat="server"> <asp:Repeater id="myRepeater" runat="server"> <HeaderTemplate> <table border="1"> <tr> <th>PictureID</th> <th>PictureName</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td align="center"><%# DataBinder.Eval(Container.DataItem, "PictureID") %></td> <td><a href="Myfiles/<%# DataBinder.Eval(Container.DataItem, "PictureName") %>" target="blank"> <%# DataBinder.Eval(Container.DataItem, "PictureName") %></a> </td> </tr> </ItemTemplate> </asp:Repeater> </form> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท