i have one table tbl_Image1
fields are: sr (int), UserName (varchar(50)), Filename (varchar(50)), FilePath (varchar(200))
my coding for image upload is as below
if (FileUpload1.PostedFile != null)
{
if (FileUpload1.PostedFile.ContentLength > (1024*1024))
{
Label37.Text = "Upload status: The file has to be less than 1 MB. Please resize your photo and than upload it again.";
}
else
{
System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);
int imageHeight = imageToBeResized.Height;
int imageWidth = imageToBeResized.Width;
int maxHeight = 660;
int maxWidth = 560;
imageHeight = (imageHeight * maxWidth) / imageWidth;
imageWidth = maxWidth;
if (imageHeight > maxHeight)
{
imageWidth = (imageWidth * maxHeight) / imageHeight;
imageHeight = maxHeight;
}
Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
System.IO.MemoryStream stream = new MemoryStream();
stream.Position = 0;
byte[] image = new byte[stream.Length + 1];
stream.Read(image, 0, image.Length);
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
bitmap.Save(Server.MapPath("profimgs/" + TextBox20.Text + "1" + extension));
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
string strQuery = "insert into tbl_Image1 (UserName, FileName, FilePath) values ('" + TextBox20.Text + "',@FileName, @FilePath)";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.CommandTimeout = 0;
cmd.Parameters.AddWithValue("@FileName", FileName);
cmd.Parameters.AddWithValue("@FilePath", "profimgs/" + TextBox20.Text + "1" + extension);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
con.Close();
con.Dispose();
Response.Redirect("~/free User/addimage.aspx");
}
}
}
my problem is just that i dont know the format fto define the virtual path for the image.
my folder is profimgs and its on the root of the server only.
have tried different things like,
bitmap.Save(Server.MapPath("profimgs/" + imagename));
bitmap.Save(Server.MapPath("~") + @"\profimgs\" + imagename);
bitmap.Save(Server.MapPath("http://www.mywebsitename.com/profimgs/" + imagename));
but none of them worked. :(
i only need to know "HOW TO SPECIFY THE VIRTUAL PATH TO STORE IN THE DATABASE FOR THE IMAGE?"