Upload and Show Profile Picture Based on Session ID in ASP.NET

This was a question asked to in asp.net forums. It asked How to Upload and Show Profile Picture Based on Session ID in ASP.NET. I found it interesting and thought to write a post for it. So here we are with the solution:

FileUpload.aspx


<form id="form1" runat="server">
<div>
    <asp:FileUpload ID="FileUploadControl" runat="server" />
    <asp:Button runat="server" ID="UploadButton" Text="Upload" OnClick="UploadButton_Click" />
    <asp:Image ID="ProfilePic" runat="server" Height="150px" Width="150px" />
    <br />
    <br />
    <asp:Label runat="server" ID="StatusLabel" Text="Upload status: " />
</div>
</form>



FileUpload.aspx.cs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FileUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserSessionID"] == null)
            Session["UserSessionID"] = System.Guid.NewGuid().ToString();//probably  create this in global.asax in OnSessionStart event

        ProfilePic.ImageUrl = "HttpHandler.ashx?ID=" + Session["UserSessionID"].ToString();
    }
    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile)
        {
            try
            {
                string ext = System.IO.Path.GetExtension(FileUploadControl.PostedFile.FileName);
                Session["UserSessionID"] = Session["UserSessionID"] + ext;
                FileUploadControl.SaveAs(Server.MapPath("~/") + Session["UserSessionID"]);
                StatusLabel.Text = "Upload status: File uploaded!";
                ProfilePic.ImageUrl = "HttpHandler.ashx?ID=" + Session["UserSessionID"].ToString();
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    }
}


HttpHandler.ashx


<%@ WebHandler Language="C#" Class="HttpHandler" %>

using System;
using System.Web;

public class HttpHandler : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {
        string imagePath = context.Request.QueryString["ID"];

        // split the string on periods and read the last element, this is to ensure we have
        // the right ContentType if the file is named something like "image1.jpg.png"
        System.IO.FileInfo imageFile = new System.IO.FileInfo(context.Request.PhysicalApplicationPath + imagePath); 
        bool fileExists = imageFile.Exists;
        if (fileExists)
        {
            context.Response.ContentType = "image/" + imageFile.Extension;
            context.Response.WriteFile(context.Server.MapPath(imagePath));
        }
        else
        {
            context.Response.ContentType = "image/jpeg";
            context.Response.WriteFile(context.Server.MapPath("NoImage.jpg"));
        }
       
            context.Response.Write(imagePath);
       
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

Popular Posts