2011-09-09 3 views
0

Je suis en train d'enregistrer un fichier image sur le serveur. Le fichier est enregistré avec succès sur le serveur mais lorsque j'essaie d'assigner l'URL de ce fichier au contrôle d'image, l'image ne peut pas être chargée mais lorsque j'affecte cette URL directement au code HTML, le fichier est chargé avec succès. S'il te plaît, guide-moi Où je fais une erreur. Voici le code pour le téléchargement de mon fichier et récupérer l'URL.Impossible de télécharger l'image enregistrée sur le serveur

code pour File Upload

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

Ceci est le code pour obtenir l'URL de l'image

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(); 

       } 
      } 

Si je Copiez et collez le chemin récupéré dans le navigateur, l'image se trouve là au serveur.

Répondre

1

Vous pouvez essayer ceci:

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(); 

    } 
} 
Questions connexes