html文件
<div class="row cl"> <label class="form-label col-xs-4 col-sm-2"> </label> <div class="formControls col-xs-8 col-sm-9"> <img src="" id="img0" style="max-width: 150px; max-height: 150px;" /> </div> </div> <div class="row cl"> <label class="form-label col-xs-4 col-sm-2"> <span class="c-red">*</span>分类logo: </label> <div class="formControls col-xs-8 col-sm-9"> <input type="file" name="pic" id="file0" multiple="multiple"> </div> </div>
js代码
<script> $("#file0").change(function(){ var objUrl = getObjectURL(this.files[0]) ; console.log("objUrl = "+objUrl) ; if (objUrl) { $("#img0").attr("src", objUrl) ; } }) ; //建立一個可存取到該file的url 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 ; } </script>