1

I have the input file upload html code as below. I want to set the input file value and then show the image in the input box. But failed. Do you know why?? Any solutions??

<div class="mb10">
<input id="image2" name="image2" value="<?php echo $imageFile2;?>" class="file" type="file" accept="image/*">
                                    <div id="uploadImgError2">
                                    </div>
                              </div>

$("#image2").fileinput({
    'showPreview' : true,
    'allowedFileExtensions' : ['jpg', 'png','gif', 'bmp'],
    'showUpload' : false,
    'maxFileCount':1,
    'maxFileSize': 10000000
});

1 Answer 1

1

HTML

<html>
<head>
<link class="jsbin" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
  article, aside, figure, footer, header, hgroup, 
  menu, nav, section { display: block; }
</style>
</head>
<body>
  <input type='file' onchange="readURL(this);" />
    <img id="image2" src="#" alt="image2" />
</body>
</html>

SCRIPT

function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#image2')
                    .attr('src', e.target.result)
                    .width(150)
                    .height(200);
            };

            reader.readAsDataURL(input.files[0]);
        }
    }

You can change the height and width as per you requirement.

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

1 Comment

Actually I still want to use fileinput to get the picture from website and preview in the file input tag. Is it possible???

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.