簡體   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