using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace fileupld
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void onbtnupldclick(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileExtension.ToLower() != ".jpg" && fileExtension.ToUpper() != ".jpeg")
{
Response.Write("<script>alert('Please Upload .jpeg Or .jpg Files only');</script>");
}
else
{
int fileSize = FileUpload1.PostedFile.ContentLength;
if (fileSize >= 1000000)
{
Response.Write("<script>alert('Please Upload less of 1 mb');</script>");
}
else
{
con.Open();
FileUpload1.SaveAs(Server.MapPath("~/folder/" + FileUpload1.FileName));
String s1 = FileUpload1.FileName;
String s2 = "insert into fileupload values('" + s1 + "')";
SqlCommand cmd = new SqlCommand(s2, con);
cmd.ExecuteNonQuery();
Response.Write("<script>alert('File Uploaded');</script>");
}
}
}
else
{
Response.Write("<script>alert('Please Select The File');</script>");
}
}
protected void btnviewclick(object sender, EventArgs e)
{
con.Open();
string sql = "select filename from fileupload";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Response.Write("<script>window.open('../Picture/"+dt.Rows[0][0].ToString()+"','-blank');</script>");
}
}
}
}