Last active
December 27, 2015 08:59
-
-
Save saintc0d3r/7300627 to your computer and use it in GitHub Desktop.
[HTML5][Jquery][ASP NET MVC] Create uploaded Image's Preview using HTML5 & JQuery on ASP NET MVC's Razor page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model SomeModel | |
@using (Html.BeginForm("Submit", "Home", FormMethod.Post, new { enctype ="multipart/form-data" })) | |
{ | |
@Html.EditorForModel() | |
<label for="ImagePreview">Image Preview</label> | |
<p> | |
<output id="image-box"></output> | |
</p> | |
<input id="imageFileUpload" name="imageFileUpload" type="file" accept="image/*" value="Browse..."/> | |
<p> <input type="submit" value="Submit"/> </p> | |
} | |
<script type="text/javascript"> | |
$(document).ready(function () { | |
$('#imageFileUpload').bind('change', onFileSelected); | |
}); | |
function onFileSelected(evt) { | |
var uploadedFile = evt.target.files[0]; | |
// Read uploaded file using HTML5 API ( FileReader ) | |
var fileReader = new FileReader(); | |
fileReader.onload = function (fileReaderEvent) { | |
var imageElement = ['<img src="', fileReaderEvent.target.result, '" title="', uploadedFile.name, '" width=', 320, ' />'].join(''); | |
$('#image-box').prop('innerHTML', imageElement); | |
}; | |
fileReader.readAsDataURL(uploadedFile); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment