开发者

File upload control error

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


SaveAs() requires an absolute path.

try using Request.PhysicalApplicationPath + "\\User"


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.


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";


 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.......

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜