views:

30

answers:

4

i am uploading image using file upload control. But it is giving the error, The SaveAs method is configured to require a rooted path, and the path '~/Admin/ProductImages/images (5).jpg' is not rooted.

string strBigServerPath = "~/Admin/ProductImages/"; string strFileName = ""; string ImageName = "";

        if (prodImg.HasFile)
        {
            strFileName = prodImg.PostedFile.FileName;             

            prodImg.PostedFile.SaveAs(imgPath + strFileName);


            string[] ext = strFileName.Split('.');
            string newProductFileName = ext[0] + "123";
            ImageName = newProductFileName + "." + ext[1];
            prodImg.PostedFile.SaveAs(imgPath + ImageName);


            using (System.Drawing.Image Img =
              System.Drawing.Image.FromFile(Server.MapPath(strBigServerPath) + newFileName))
            {
                if (Img.Width > 250 && Img.Height > 400)
                {
                    Size MainSize = new Size(250, 300);
                    using (System.Drawing.Image ImgThnail =
                           new Bitmap(Img, MainSize.Width, MainSize.Height))
                    {

                        ImgThnail.Save(Server.MapPath(strBigServerPath) + ImageName);

                    }
                }
                Img.Dispose();
            }
A: 

You should pass an absolute path to the SaveAs() method. The HostingEnvironment.ApplicationPhysicalPath Property is a convenient way to get the root folder of your web application.

var path = HostingEnvironment.ApplicationPhysicalPath + imgPath + strFileName;
prodImg.PostedFile.SaveAs(path); 
Jakob Gade
A: 

Maybe this helps:

string filePath = System.IO.Path.Combine(Server.MapPath(imgPath), strFileName);
prodImg.PostedFile.SaveAs(filePath);
Zafer
A: 

When using this:

prodImg.PostedFile.SaveAs(path);

the parameter path must be an absolute path that you could resolve with this:

Server.MapPath(strBigServerPath + filename)
Wouter Janssens - Xelos bvba
A: 

is it due to you using

prodImg.PostedFile.SaveAs(imgPath + strFileName);

when it should be

prodImg.PostedFile.SaveAs(strBigServerPath + strFileName);
Ashley
Also as others point out you can turn the relative path to an absolute one using the Server.MapPath() method.
Ashley