i know that with the same name we does not store two file....
the solution is GUID number which generates a new name for the image ...
it happens internally.... but user files that he storing the image with same name.....
but my problem is i know abt the properties of GUID ...
but i does not now how to use guid to store the image.............
Loading
Deepak PandeyPosted Jun 13, 2010, 12:52 AM
using System;
using System.IO;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class FileUpload : System.Web.UI.Page
{
SqlCommand cmd;
SqlConnection con;
string path, image_name,q;
SqlDataAdapter sda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
image_display();
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (!Directory.Exists(Server.MapPath("images")))
{
Directory.CreateDirectory(Server.MapPath("images"));
if (imageUpload.FileName != "")
{
store_image();
}
else
{
Response.Write("Select An Image For Upload");
}
}
else
{
if (imageUpload.FileName !="")
{
store_image();
}
else
{
Response.Write("Select An Image For Upload");
}
}
}
public void store_image()
{
image_name = Guid.NewGuid().ToString() + imageUpload.FileName.Substring(imageUpload.FileName.LastIndexOf("."));
path = Server.MapPath("images");
if (path.EndsWith("\\") == false)
{
path += "\\";
}
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ToString());
cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into store_image values(@image_name)";
cmd.Parameters.AddWithValue("@image_name", image_name);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
if (imageUpload.HasFile)
{
if (checkFileType(imageUpload.FileName))
{
imageUpload.PostedFile.SaveAs(path + "\\" + image_name);
cmd.ExecuteNonQuery();
image_display();
Response.Write("image uploaded");
}
else
{
Response.Write("Upload Only .jpeg,.bmp,.png,.jpg files");
}
}
}
public void image_display()
{
q = "select * from store_image";
con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
con.Open();
ds = new DataSet();
sda = new SqlDataAdapter(q, con);
sda.Fill(ds);
dlImage.DataSource = ds.Tables[0];
dlImage.DataBind();
}
bool checkFileType(string filename)
{
string ext = Path.GetExtension(filename);
switch (ext)
{
case ".gif": return true;
case ".bmp": return true;
case ".jpeg": return true;
case ".jpg": return true;
case ".png": return true;
case ".GIF": return true;
case ".BMP": return true;
case ".JPEG": return true;
case ".JPG": return true;
default: return false;
}
}
}
CrishPosted Jun 11, 2010, 5:13 AM
you can upload same name image but once uploaded same name image then after it overwrites the older one.
you can check same name image exist from folder. and rename the image name for upload.
thanks