IMAGE UPLOAD AND SAVE PATH IN SQL SERVER
IMAGE UPLOAD AND SAVE PATH IN SQL SERVER
code.aspx<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
code.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;
public Default4()
{
con = new SqlConnection("initial catalog=OliverbonasDB; data source=om-pc; integrated security=yes");
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
string fname = DateTime.Now.ToShortTimeString().GetHashCode().ToString() + FileUpload1.PostedFile.FileName;
string filepath = Server.MapPath(@"~/images/" + fname);
FileUpload1.PostedFile.SaveAs(filepath);
//save file name to db
con.Open();
cmd = new SqlCommand("insert into ImageSave(savedfile) values('" + fname + "')", con);
int t= cmd.ExecuteNonQuery();
if (t > 0)
{
Console.WriteLine("record saved");
}
con.Close();
}
catch(Exception ex)
{
Label1.Text = "Record is NOT saved";
}
}
}
}
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home