Preview Files Before Upload using jQuery in ASP.Net

Last Reply 5 months ago By pandeyism

Posted 5 months ago


i am using this file upload solution, i want as user attached file it should preview so there are 3 cases

1. Image - so bind image in image control as soon as user attached the image and before click on upload button so  user can see the preview

2. Video- so bind the video in html5 video control so user can see the preview before upload

3. File (word pdf ppt) in this case user can see the ICON that a file is attached.

 <style type="text/css">
            font-family: Arial;
            font-size: 10pt;
<input type="file" name="postedFile" />
<input type="button" id="btnUpload" value="Upload" />

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<progress id="fileProgress" style="display: none"></progress>
<hr />
<span id="lblMessage" style="color: Green"></span>
<script type="text/javascript" src=""></script>
<script type="text/javascript">
    $("body").on("click", "#btnUpload", function () {
            url: '<%= ResolveUrl("~/HandlerCS.ashx")%>',
            type: 'POST',
            data: new FormData($('form')[0]),
            cache: false,
            contentType: false,
            processData: false,
            success: function (file) {
                $("#lblMessage").html("<b>" + + "</b> has been uploaded.");
            xhr: function () {
                var fileXhr = $.ajaxSettings.xhr();
                if (fileXhr.upload) {
                    fileXhr.upload.addEventListener("progress", function (e) {
                        if (e.lengthComputable) {
                                value: e.loaded,
                    }, false);
                return fileXhr;


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

using System;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using socialnetworkingwebsite.Models;

public class HandlerCS : IHttpHandler
    smEntities db = new smEntities();
    public void ProcessRequest(HttpContext context)
        //Check if Request is to Upload the File.
        if (context.Request.Files.Count > 0)
            //Fetch the Uploaded File.
            HttpPostedFile postedFile = context.Request.Files[0];
            //Set the Folder Path.
            string folderPath = context.Server.MapPath("~/DynamicImages/");

            //Set the File Name.
            string fileName = Path.GetFileName(postedFile.FileName);
            //Save the File in Folder.
            postedFile.SaveAs(folderPath + fileName);

            //Send File details in a JSON Response.
            string json = new JavaScriptSerializer().Serialize(
                    name = fileName
            context.Response.StatusCode = (int)HttpStatusCode.OK;
            context.Response.ContentType = "text/json";

    public bool IsReusable
            return false;
You are viewing reply posted by: pandeyism 5 months ago.