ASP.NET(vb.net) & Upload Resize Insert to Database - The in this tutorial, you’ll learn and example scripts how to Upload and resize insert into database using by ASP.NET scripts.
ShotDev Focus:
- ASP.NET(vb.net) & Upload Resize Insert to Database
Example
AspNetUploadResizeDB1.aspx
<%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="VB" %> <script runat="server"> Dim objConn As OleDbConnection Dim objCmd As OleDbCommand Sub btnUpload_OnClick(sender As Object, e As EventArgs) If Me.fiUpload.HasFile Then Dim intWidth,intHeight As Integer Dim UlFileName,NewFileName As String intWidth = 100 '*** Fix Width ***' 'intHeight = 0 '*** If = 0 Auto Re-Cal Size ***' intHeight = 120 UlFileName = "MyImages/" & fiUpload.FileName '*** Save Images ***' Me.fiUpload.SaveAs(Server.MapPath(UlFileName)) NewFileName = "MyImages/Thumbnail_" & fiUpload.FileName Dim objGraphic As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(UlFileName)) Dim objBitmap As Bitmap '*** Calculate Height ***' If intHeight > 0 Then objBitmap = New Bitmap(objGraphic, intWidth, intHeight) Else If objGraphic.Width > intWidth Then Dim ratio As Double = objGraphic.Height / objGraphic.Width intHeight = ratio * intWidth objBitmap = New Bitmap(objGraphic, intWidth, intHeight) Else objBitmap = New Bitmap(objGraphic) End If End If '*** Save As ***' objBitmap.Save(Server.MapPath(NewFileName), objGraphic.RawFormat) '*** Close ***' objGraphic.Dispose() '*** Nothing ***' objBitmap = Nothing objGraphic = Nothing '*** Save to Database ***' Dim objConn As OleDbConnection Dim objCmd As OleDbCommand Dim strConnString,strSQL As String strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";" objConn = New OleDbConnection(strConnString) objConn.Open() strSQL = "INSERT INTO images (ImagesName,Thumbnails) " & _ "VALUES ('" & UlFileName & "','" & NewFileName & "')" objCmd = New OleDbCommand() With objCmd .Connection = objConn .CommandType = CommandType.Text .CommandText = strSQL End With objCmd.ExecuteNonQuery() objCmd = Nothing objConn.Close() objConn = Nothing '*** Save to Database (End) ***' '*** View Images ***' Me.imgPicture.Visible = True Me.imgPicture.ImageURL = NewFileName '*** Hyperlink ***' Me.hplLink1.Visible = True Me.hplLink1.NavigateUrl = "AspNetUploadResizeDB2.aspx" End IF End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:FileUpload id="fiUpload" runat="server"></asp:FileUpload> <input id="btnUpload" type="button" OnServerClick="btnUpload_OnClick" value="Upload" runat="server" /> <hr /> <asp:Image id="imgPicture" Visible="false" runat="server" /><br /><br /> <asp:HyperLink id="hplLink1" Visible = "false" runat="server">View files</asp:HyperLink> </form> </body> </html>
AspNetUploadResizeDB2.aspx
<%@ Import Namespace="System.Data"%> <%@ Import Namespace="System.Data.OleDb"%> <%@ Page Language="VB" %> <script runat="server"> Dim objConn As OleDbConnection Dim objCmd As OleDbCommand Sub Page_Load(sender As Object, e As EventArgs) Dim strConnString As String strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="&Server.MapPath("database/mydatabase.mdb")&";" objConn = New OleDbConnection(strConnString) objConn.Open() BindData() End Sub Sub BindData() Dim strSQL As String strSQL = "SELECT * FROM images" Dim dtReader As OleDbDataReader objCmd = New OleDbCommand(strSQL, objConn) dtReader = objCmd.ExecuteReader() '*** BindData to DataList ***' myDataList.DataSource = dtReader myDataList.DataBind() dtReader.Close() dtReader = Nothing End Sub Sub Page_UnLoad() objConn.Close() objConn = Nothing End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form id="form1" runat="server"> <asp:DataList id="myDataList" RepeatColumns="2" runat="server"> <HeaderTemplate> <b>My Girl</b> </HeaderTemplate> <ItemTemplate> <div style="width:100px" align="center"> <a href="<%#Container.DataItem("ImagesName")%>" target="blank"> <img src="<%#Container.DataItem("Thumbnails")%>" border="0"> </a> </div> </ItemTemplate> <SeparatorTemplate> <hr /> </SeparatorTemplate> </asp:DataList> </form> </body> </html>
Screenshot