开发者

Failed to upload image saved on server

I am saving an image file at server. The file is successfully saved at the server but when I try to assign the URL of that file to the image control, the image is failed to load but when I assign that url directly in to HTML code, the file is loaded successfully. Please guide me Where I am making a mistake. Below are the code for my file upload and fetch URL.

Code For 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.SaveA开发者_运维百科s(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;
    }
}

This is the code to get the image 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();

                }
            }

If I copy and past the retrieved path in the browser, the image is found there at the server.


You may try this:

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

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜