- <div class="form-group">
- <label for="img">图片</label>
- <input type="file" id="upload" name="img" class="" id="img" >
- </div>
- <div class="form-group" style="display: block">
- <img src="" alt="" class="img-rounded" id="pic" style="width: 140px;height: 140px;">
- </div>
- $(function(){
- $("#upload").on("change",function(){
- var objUrl = getObjectURL(this.files[0]) ; //获取图片的路径,该路径不是图片在本地的路径
- if (objUrl) {
- $("#pic").attr("src", objUrl) ;
- }
- });
- });
- function getObjectURL(file) {
- var url = null ;
- if (window.createObjectURL!=undefined) { // basic
- url = window.createObjectURL(file) ;
- } else if (window.URL!=undefined) { // mozilla(firefox)
- url = window.URL.createObjectURL(file) ;
- } else if (window.webkitURL!=undefined) { // webkit or chrome
- url = window.webkitURL.createObjectURL(file) ;
- }
- return url ;
- }
来源: http://blog.csdn.net/qq_16618179/article/details/78069177