0

I have this code (please see below), when the user is selecting an image i'd like to display a preview of the image. It works well for standard formats but since tiff is not supported by some of the browser it will show a broken image if user is selecting a tiff image. I have no problem converting/encoding the image when i'm getting it from the server my issue is on the client side.

The relevant javascript:

$(function () {
$(document).on('change', '#ImageData', function (evt) {
    try {
        var file = evt.target.files[0] || evt.srcElement.files[0]; // Read  the first file from the list.
    }
    catch (e) {
        // Something went wrong or no file was chosen - 
        // remove current image and return.
        removeImg();
        return;
    }

    if (isImage(file)) { // Add only valid images
        var reader = new FileReader();
        reader.onloadend = (function () {
            return function (event) {
                var img = document.getElementById('img-id');
                img.src = event.target.result;
            };
        })(file);
        reader.readAsDataURL(file); // Read in the image file as a data URL.
    }
    else {
        removeImg(); // If there is already image remove it
    }
});
});


The relevant html:
@Html.ValidationMessageFor(model => model.imageFile, "", new { @class = "text-danger" })
@Html.TextBoxFor(model => model.imageFile, new { type = "file", name ="imageFile", id = "ImageData" })
<img id='img-id' style="height: 300px; width: auto;">
SBS21
  • 1
  • [This SO question](http://stackoverflow.com/questions/17979696/javascript-tiff-image-conversion) and [this answer](http://stackoverflow.com/a/16311760) might be of interest to you (btw, haven't you even googled for your problem ?) – collapsar Mar 10 '15 at 07:12
  • Replace "something went wrong" with the actually exception – radbyx Mar 10 '15 at 07:12
  • Hi collapsar, yes i have googled and came across this answers but i just need something simple for the preview these solutions are too complicated and in this case i prefer just to display the file name. Thanks anyway for your comment. – SBS21 Mar 10 '15 at 08:41
  • radbyx thanks for the comment not exactly solving my issue but useful :-) – SBS21 Mar 10 '15 at 08:47

0 Answers0