3

I am trying to preview an image that the user is trying to upload. This is the code i am using.This is working fine in all major browsers, but i am wondering if there is a better way to achieve this without using session(even though i am clearing the session ) ? I don't want to use Flash.

   @using (Html.BeginForm("UploadImage", "Home", FormMethod.Post, new { id = "fileuploadform", @enctype = "multipart/form-data" }))
    {
        <input id="fileupload" type="file" onchange="ChangeImage()" name="files" multiple>
        <div id="preview">
            <img src="#" id="imgThumbnail" alt="preview" />
        </div>

    }
            <script>
            $(function ChangeImage() {
                $('#fileuploadform').fileupload({
                    done: function (e, data) {
                        var d = new Date();
                        $('#imgThumbnail')[0].src = "/Home/ImageLoad?a=" + d.getTime();
                    }
                });
            });
        </script>

[HttpPost]
public ActionResult UploadImage(IEnumerable<HttpPostedFileBase> files, PostViewModel model)
{

    Session["ContentLength"] = Request.Files[0].ContentLength;
    Session["ContentType"] = Request.Files[0].ContentType;
    byte[] b = new byte[Request.Files[0].ContentLength];
    Request.Files[0].InputStream.Read(b, 0, Request.Files[0].ContentLength);
    Session["ContentStream"] = b;
    return Content(Request.Files[0].ContentType + ";" + Request.Files[0].ContentLength);
}

   public ActionResult ImageLoad(int? id)
    {
        byte[] b = (byte[])Session["ContentStream"];
        int length = (int)Session["ContentLength"];
        string type = (string)Session["ContentType"];
        Session["ContentLength"] = null;
        Session["ContentType"] = null;
        Session["ContentStream"] = null;
        return File(b, type);
    }

3 Answers 3

3

Have you tried this solution?

Preview an image before it is uploaded

This solution is purely client side, and does not needed to be uploaded to your server before preview. :)

Sign up to request clarification or add additional context in comments.

Comments

1

You could improve your action's code in this way:

HttpPostedFileBase picFile = Request.Files["fileUpload"];
if (picFile != null && picFile.ContentLength > 0)
{
     byte[] pic = new byte[picFile.ContentLength];
     picFile.InputStream.Read(pic, 0, picFile.ContentLength);
     // you can use pic ....
}

Comments

1

Preview image using Fileupload Asp.Net C# MVC with Razor Framework and jQuery?

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.");
    }
});

I hope this will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.