Preview image using Fileupload asp.net mvc and Jquery(sưu tầm từ stackoverflow.com)

This will preview multiple files as thumbnail images at a time.
On View Asp.Net C# MVC with Razor Framework
<div class="form-group">
   @Html.LabelFor(model => model.ImageMedias)
   <div class="input-group">
      <div class="custom-file">
         @Html.TextBoxFor(model => model.ImageMedias, "", new { @class = "custom-file-input", @type = "file", @multiple = "multiple", @accept = ".jfif,.jpg,.jpeg,.png,.gif" })
         <label class="custom-file-label" for="InputFile">Choose file</label>
      </div>
   </div>
   @Html.ValidationMessageFor(model => model.ImageMedias)   
   <div id="divImageMediaPreview">
   </div>
</div>
Script
$("#ImageMedias").change(function () {
    if (typeof (FileReader) != "undefined") {
        var dvPreview = $("#divImageMediaPreview");
        dvPreview.html("");            
        $($(this)[0].files).each(function () {
            var file = $(this);                
                var reader = new FileReader();
                reader.onload = function (e) {
                    var img = $("<img />");
                    img.attr("style", "width: 150px; height:100px; padding: 10px");
                    img.attr("src", e.target.result);
                    dvPreview.append(img);
                }
                reader.readAsDataURL(file[0]);                
        });
    } else {
        alert("This browser does not support HTML5 FileReader.");
    }
});

Nhận xét

Bài đăng phổ biến từ blog này

Khôi phục phân vùng ổ cứng do ghost nhầm theo Hieuit.net

Cách sử dụng 2 phương thức [httppost] và [httpget] trong MVC theo https://dzungdt.wordpress.com

MVC định dạng tiền tệ vnđ - Format currency vnd in MVC