How to upload / store images in ASP
How to upload or insert image in ASP.NET
We will be storing the image name and the path in the database and moving the image file in a folder called "uploads".
We will use the "FileUpload" tool from the toolbox to upload the image.
<form id="form1" runat="server">
<label>Upload Product Image</label>
<asp:FileUpload ID="p_image" Required="True" class="form-control" runat="server" />
<asp:Button ID="addProductBtn" runat="server" class="btn btn-primary" Text="Save Product" />
We will fetch the image in the code behind on button click action, when the user clicks the submit button.
We have to create a folder and name it as "uploads" and move the uploaded images to that folder using below code:
Protected Sub addProductBtn_Click(sender As Object, e As EventArgs) Handles addProductBtn.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim path As String
con.ConnectionString = "YOUR_CONNECTION_STRING_HERE"
cmd.Connection = con
If p_image.HasFile Then
path = p_image.FileName
p_image.SaveAs(Server.MapPath("uploads/") + path)
path = "uploads/" + path
Response.Write("NO FILE SELECTED")
cmd = New SqlCommand("INSERT INTO products (image) values('" & path & "')", con)
MsgBox("Product added successfully", MsgBoxStyle.Information, "Success")