0

im using a file upload control and here is my code :

//Uploading the image
     if (imageUpload.HasFile) 
     {
         try
         {
             if (imageUpload.PostedFile.ContentType == "image/jpeg")
             {
                 if (imageUpload.PostedFile.ContentLength < 102400)
                 {
                     string im = ( "~/User" +  "/" + Page.User.Identity.Name + "/" + Page.User.Identity.Name  + ".jpeg");
                     imageUpload.SaveAs(im);
                     uploadLabel.Text = "";
                 }
                 else
                 {
                     uploadLabel.Text = "File size must be less than 1024 kb";
                 }
             }
             else 
             {
                 uploadLabel.Text = "File must be in jpeg/jpg format"; 
             }
         }
         catch(Exception ex) 
         {
             uploadLabel.Text = "File upload failed becuase: " + ex.Message; 
         }
     }

but im getting an error: The SaveAs method is configured to require a rooted path, and the path "path" is not rooted.

what am i doing wrong. thanks

0

4 Answers 4

1

SaveAs() requires an absolute path.

try using Request.PhysicalApplicationPath + "\\User"

Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to notify you that I'm undeleting your answer since @Wahtever said that this is what helped him solve the problem.
1

The Save method is configured to require an absolute path (starting with X:\..., in some drive).

You should call Server.MapPath to get the absolute path on disk to ~/whatever.

Comments

0

Add Server.MapPath where you declare im Server.MapPath gives you the absolute path.

string im = Server.MapPath("/User") + "/" + Page.User.Identity.Name + "/" + Page.User.Identity.Name + ".jpeg";

Comments

0
 string filename = FileUpload1.FileName.ToString();
     if (filename != "")
        {             
            ImageName = FileUpload1.FileName.ToString();

            ImagePath = Server.MapPath("Images");
            SaveLocation = ImagePath + "\\" + ImageName;
            SaveLocation1 = "~/Image/" + ImageName;
            sl1 = "Images/" + ImageName;
            FileUpload1.PostedFile.SaveAs(SaveLocation);
         }

try this may be help ful.......

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.