Click here to Skip to main content
16,017,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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();
          // bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
           stream.Position = 0;
           byte[] image = new byte[stream.Length + 1];
           stream.Read(image, 0, image.Length);

           string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
           //Save files to disk
           string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
           //Path.Combine(Server.MapPath("~/images/store"), imageName);
           //string imagename = TextBox20.Text + "1" + extension;
           bitmap.Save(Server.MapPath("profimgs/" + TextBox20.Text + "1" + extension));
           //Add Entry to DataBase


           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,

C#
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?"
Posted
Updated 6-Mar-12 23:26pm
v2

By using Server.MapPath() you are not using the virtual path.
So "~/Yourpath/" should be format to use as value in your databasetable.
 
Share this answer
 
Comments
patelKhyati 7-Mar-12 5:40am    
i can use "~/Yourpath/" to store the image path in the database to display the image.

but which path to use to store the image on the folder profimgs on my server? we have to save the image in the folder first and than save only its path to database.
and to store the image on the server folder, i have to use Server.MapPath()
Member 12929242 10-Mar-17 5:32am    
i want something like this i'm uploading image it is working in local database but when i publish this project, in online link the images are not getting ??
Server.MapPath(~/profimgs/")

will save/retrieve the images in a folder "profimgs" on root of the website.
 
Share this answer
 
Comments
patelKhyati 7-Mar-12 6:19am    
have tried this, its not working i dont know why
Herman<T>.Instance 7-Mar-12 6:39am    
is your folder within the IIS structure?
patelKhyati 7-Mar-12 7:20am    
i dont know how to do that as i am a fresher.
its create a folder in your web site and put all images on these folder

if (hpf1.ContentLength > 0)
{

if (!Directory.Exists(Server.MapPath() + "/yourfoldername/))
{
Directory.CreateDirectory(Server.MapPath("") + "/yourfoldername/");
}
hpf1.SaveAs(Server.MapPath("") + "/yourfoldername/" + System.IO.Path.GetFileName(hpf1.FileName));
}
and sending the file name in database
use
string filename=hpf1.FileName;
 
Share this answer
 
v3
try this :

        path = Server.MapPath("images/Photo/");
        
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(path); 
            image_path = "~/images/Photo/" + FileUpload1.Filename;
        }

       string s="insert into tablename values('"+image_path+"')";
       sqlcommand cmd=new sqlcommand(s,connection_object);
       cmd.execuetnonquery();

if you are trying to add image from admin panel or else you just change the path

 path = Server.MapPath("../images/Photo/");

hope this will help you.if not please post it.
Thanks.
 
Share this answer
 
v2
Please provide the complete code
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900