简体   繁体   中英

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 .

添加Server.MapPath你声明的地方Server.MapPath给你绝对路径。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM