2011-09-09 79 views
0

我正在服務器上保存圖像文件。該文件已成功保存在服務器上,但是當我嘗試將該文件的URL分配給圖像控件時,圖像無法加載,但是當我將該網址直接分配給HTML代碼時,該文件已成功加載。請指導我在哪裏我犯了一個錯誤。以下是我的文件上傳和抓取網址的代碼。無法上傳保存在服務器上的圖像

代碼文件上傳

private string ImageUpload() 
{ 
    try 
    { 
     string FileName = UpldCompanyLogo.FileName; 
     if (UpldCompanyLogo.HasFile) 
     { 
      string SaveFilePath = Server.MapPath("~\\Upload\\")+FileName; 
      if (!Directory.Exists(Server.MapPath("~\\Upload\\"))) 
       Directory.CreateDirectory(Server.MapPath("~\\Upload\\")); 

      if (File.Exists(SaveFilePath)) 
      { 
       File.Delete(SaveFilePath); 
      } 
      if(File.Exists(ViewState["ImageURL"].ToString())) 
      { 
       File.Delete(ViewState["ImageURL"].ToString()); 
      } 
      UpldCompanyLogo.PostedFile.SaveAs(SaveFilePath); 
     } 
     return FileName; 

    } 
    catch (Exception ex) 
    { 

     if (ex.HelpLink == null) 
      ex.HelpLink = "Controls_Company103>>" + ex.Message; 
     else 
      ex.HelpLink = "Controls_Company103>>" + ex.HelpLink; 
     lblMessage.Text = ex.HelpLink; 
     lblMessage.CssClass = "ERROR"; 
     return null; 
    } 
} 

這是代碼來獲取圖像URL

if (dtCompany != null) 
      { 
       if (dtCompany.Rows.Count > 0) 
       { 
        txtCompanyName.Text = dtCompany.Rows[0]["CompanyName"].ToString(); 
        txtAddress.Text = dtCompany.Rows[0]["Address"].ToString(); 
        txtPhoneNo.Text = dtCompany.Rows[0]["PhoneNumber"].ToString(); 
        txtFaxNo.Text = dtCompany.Rows[0]["FaxNumber"].ToString(); 
        string path = Server.MapPath("~\\Upload\\"); 
        imgLogo.ImageUrl = path + dtCompany.Rows[0]["CompanyLogo"].ToString(); 

       } 
      } 

如果我複製和過去在瀏覽器中檢索路徑,圖像是在那裏發現服務器。

回答

1

你可以試試這個:

if (dtCompany != null) 
{ 
    if (dtCompany.Rows.Count > 0) 
    { 
     txtCompanyName.Text = dtCompany.Rows[0]["CompanyName"].ToString(); 
     txtAddress.Text = dtCompany.Rows[0]["Address"].ToString(); 
     txtPhoneNo.Text = dtCompany.Rows[0]["PhoneNumber"].ToString(); 
     txtFaxNo.Text = dtCompany.Rows[0]["FaxNumber"].ToString(); 
     imgLogo.ImageUrl = Page.ResolveUrl("~\\Upload\\") + dtCompany.Rows[0]["CompanyLogo"].ToString(); 

    } 
} 
相關問題