0

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();
            }

4 Answers 4

0

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); 
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe this helps:

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

Comments

0

is it due to you using

prodImg.PostedFile.SaveAs(imgPath + strFileName);

when it should be

prodImg.PostedFile.SaveAs(strBigServerPath + strFileName);

1 Comment

Also as others point out you can turn the relative path to an absolute one using the Server.MapPath() method.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.