繁体   English   中英

文件上传控制错误

[英]File upload control error

即时通讯使用文件上传控件,这是我的代码:

//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; 
         }
     }

但我得到一个错误:SaveAs方法配置为需要一个根路径,路径“路径”不是root。

我究竟做错了什么。 谢谢

SaveAs()需要一个绝对路径。

尝试使用Request.PhysicalApplicationPath + "\\\\User"

Save方法配置为需要绝对路径(在某些驱动器中以X:\\...开头)。

你应该调用Server.MapPath来获取磁盘上的绝对路径~/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);
         }

试试这可能有助于......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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