Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have image upload functionality in which the uploaded image should be saved into a system folder. The uploaded image should also get displayed in the page, to get the image from the system folder I have used the help of an ashx and the image is getting loaded correctly at the time of page load. Now I want to dynamically get the image on gdvPartner_SelectedIndexChanged. Please help.

What I have tried:

ASP.NET
<tr align="center" style="height:40">
<td class="FormLabels" align="right" style="vertical-align: top">Upload Logo&nbsp;:&nbsp;&nbsp;</td>
    <td align="Left" style="vertical-align: top;width: 30%;padding:5px" colspan="2">
        <telerik:RadAsyncUpload ID="flUpload" runat="server" MaxFileInputsCount="1" AllowedFileExtensions=".png"
            Skin="Web20" Width="200px" />
        <asp:Button ID="btnUpload" runat="server" Style="text-decoration: none; border-radius: 5px;
            border: solid 1px #f39c12;" Text="Upload" CssClass="submitButton" SkinID="Submit"
            OnClick="btnUpload_Click" />
    </td>
    <td  align="Left" colspan="2" style="height: 180px;width: 35%;padding-left:10px">
        <asp:Image Width="200px" Height="150px" runat="server" ImageUrl="../UserControl/GetImage.ashx"
            ID="imgSelectedPhoto" />
    </td>
</tr>




C#
   protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (flUpload.UploadedFiles.Count != 0)
        {
                SaveAsThumbNail();
        }
    }

/// <summary>
/// for saving employee photo
/// </summary>
private void SaveAsThumbNail()
{
    Common cmnObj = new Common();
    try
    {
        string serPath = Utilities.DecryptString(cmnObj.RetriveKeyFromSettingsTable("PartnerLogoPath"));
        string newFileName = SessionHandler.TempPartnerId;
        newFileName = newFileName + "." + ImageFormat.Png.ToString();
        string imgPath = serPath + "/" + newFileName;
        flUpload.UploadedFiles[0].SaveAs(imgPath);//Saving file
    }
    catch (Exception ex)
    {
        BasePage bp = new BasePage();
        bp.GetErrorMessage("", "", ex, Page.Title, "SaveAsThumbNail");
    }
}





C#
public void ProcessRequest(HttpContext context)
{
    string imgPath = string.Empty;
    string extension = string.Empty;

    Common cmnObj = new Common();
    string serPath = Utilities.DecryptString(cmnObj.RetriveKeyFromSettingsTable("PartnerLogoPath"));

    string newFileName = SessionHandler.TempPartnerId;
    newFileName = newFileName + "." + ImageFormat.Png.ToString();
    imgPath = serPath + "/" + newFileName;
    if (File.Exists(imgPath))
    {
            string contentType = string.Empty;
            imgPath = Convert.ToString(imgPath);
            extension = imgPath.Substring((imgPath.Length - 4), 4).ToLower();
            switch (extension)
            {
                case ".png":
                    contentType = "image/png";
                    break;
                default:
                    break;
            }

        context.Response.ContentType = contentType;
        context.Response.TransmitFile(imgPath);
    }
    else
    {
        context.Response.TransmitFile("~/Images/NoImage.gif");
    }
}



C#
protected void gdvPartner_SelectedIndexChanged(object sender, EventArgs e)
{

C#
string siteHandlerUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/") + "UserControl/GetImage.ashx";
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create(siteHandlerUrl);
//HttpWebResponse response = (HttpWebResponse)request.GetResponse();
imgSelectedPhoto.ImageUrl = Page.ResolveUrl(siteHandlerUrl);

}
Posted
Updated 5-May-16 21:43pm
v2
Comments
[no name] 12-Mar-16 4:40am    
Are you getting any error? What is problem exactly means what are you expecting but not working

1 solution

Based on your code above, your ashx is actually transmitting file rather than a output stream so that your image control can convert the output stream into an image.

Someone has posted a similar solution below:
asp.net - C# - Loading remote image and sending to browser using .ashx file - Stack Overflow[^]


Hope it helps!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900