Reputation: 2000
I am working in ASP.NET and C#.
I have an Fileupload
control in my application. I want to change the storage path (on the hard disk) of the image during the upload. How do I go about it.
C# code:
string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
//Save images into Images folder
fileuploadimages.SaveAs(Server.MapPath("**~/database/**" + filename));
//Getting dbconnection from web.config connectionstring
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
//Open the database connection
con.Open();
//Query to insert images path and name into database
SqlCommand cmd = new SqlCommand("Insert into tblImageupload1(Imagename,Imagepath) values(@ImageName,@ImagePath)", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@Imagename", filename);
cmd.Parameters.AddWithValue("@Imagepath", "../Database/" + filename);
//cmd.Parameters.Add("@price");
cmd.ExecuteNonQuery();
//Close dbconnection
con.Close();
Note: I want to change the path ~/database/
dynamically during upload.
Upvotes: 0
Views: 1678
Reputation: 26912
You say "While i am Uploading i need change.Because I have separate folders for different types of images", so you probably have a way to detect the type of the image. Let's assume that you have detected the type of the currently uploaded image and stored it in the variable imagetype
.
string imagetype = ""; // type of image is here, i.e. "Bird", "Forest", "Mountain"
string imagepath = "/images";
switch (imagetype)
{
case "Bird": imagepath = "/ImagesOfBirds"; break;
case "Forest": imagepath = "/ImagesOfForests"; break;
case "Mountain": imagepath = "/ImagesOfMountains"; break;
}
string path = Path.Combine(imagepath, filename);
fileuploadimages.SaveAs(path);
Upvotes: 1