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
Đăng nhận xét