Display Icon in preview except Image file while uploading using jQuery in ASP.Net

Last Reply 7 months ago By arunkurmi

Posted 7 months ago

i have this file upload code it works well with jpg, png, gif

i want if user upload pdf, excel or word file so in preview it shows pdf, excel or word icon since documnet preview cannot be show

please advice

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('[id*=fileUploadBanner]').change(function () {
            var image = $(this).closest('DIV').find($('[id*=imgBanner]'));
            if (typeof (FileReader) != "undefined") {
                var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
                $($(this)[0].files).each(function () {
                    var file = $(this);
                    if (regex.test(file[0].name.toLowerCase())) {
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            $(image).attr("src", e.target.result).attr("style", "height:100px;width: 100px;");
                        }
                        reader.readAsDataURL(file[0]);
                    } else {
                        alert(file[0].name + " is not a valid image file.");
                        return false;
                    }
                });
            } else {
                alert("This browser does not support HTML5 FileReader.");
            }
        });
    });
</script>
<div class="white-bg shadow">
    <label class="file-upload">
        <span class="border radius padding15">Select Image</span>
        <asp:FileUpload ID="fileUploadBanner" runat="server" />
    </label>
    <span class="pull-right">
    <asp:Image ID="imgBanner" runat="server" Width="100px" /></span>
</div>
You are viewing reply posted by: arunkurmi 7 months ago.
Posted 7 months ago Modified on 7 months ago

Hi nauna,

Refer below code.

HTML

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('[id*=fileUploadBanner]').change(function () {
            var image = $(this).closest('DIV').find($('[id*=imgBanner]'));
            if (typeof (FileReader) != "undefined") {
                var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp|.pdf|.xls|.xlsx)$/;
                $($(this)[0].files).each(function () {
                    var file = $(this);
                    if (regex.test(file[0].name.toLowerCase())) {
                        var extension = file[0].name.substr((file[0].name.lastIndexOf('.') + 1));
                        var reader = new FileReader();
                        reader.onload = function (e) {
                            switch (extension) {
                                case 'jpg':
                                case 'jpeg':
                                case 'gif':
                                case 'png':
                                case 'bmp':
                                    $(image).attr("src", e.target.result).attr("style", "height:100px;width: 100px;");
                                    break;
                                case 'pdf':
                                    $(image).attr("src", "Icons/PDF.png").attr("style", "height:30px;width: 30px;");
                                    break;
                                case 'xls':
                                case 'xlsx':
                                    $(image).attr("src", "Icons/EXCEL.png").attr("style", "height:30px;width: 30px;");
                                    break;
                                default:
                            }
                        }
                        reader.readAsDataURL(file[0]);
                    } else {
                        alert(file[0].name + " is not a valid image file.");
                        return false;
                    }
                });
            } else {
                alert("This browser does not support HTML5 FileReader.");
            }
        });
    });
</script>
<div class="white-bg shadow">
    <label class="file-upload">
        <span class="border radius padding15">Select Image</span>
        <asp:FileUpload ID="fileUploadBanner" runat="server" />
    </label>
    <br />
    <span class="pull-right">
        <asp:Image ID="imgBanner" runat="server" Width="100px" /></span>
</div>