javascript - input type=file上传的图片怎样获取图片路径给img的src属性
PHP中文网
PHP中文网 2017-04-10 15:03:34
[JavaScript讨论组]

部分html代码

<p id="upload">
    <form action="{:U('Index/uploadhandler')}" enctype="multipart/form-data" method="post" id="imgform">    
            <p id="imglist">

            </p>
            <p id="upp">
            <input type='file'  name='photos' class="upfile">
            </p>              
            <input type="submit" value="提交">
    </form>
</p>

jq代码

$(function(){
        var imgurl=[];
        function createfile(){
        $('#upp').append("<input type='file'  name='photos' class='upfile'>");
        }
        function showimg(url){
            var img='<img src="'+url+'"/>';
            $('#imglist').append(img);
        }
        function addfile(){
            showimg($(this).val());
            $(this).hide();
            createfile();
            $('.upfile').bind('change',addfile);
        }
        $('.upfile').bind('change',addfile);
    })
PHP中文网
PHP中文网

认证0级讲师

全部回复(4)
PHP中文网

目前我在内部项目中是这样使用的

var objUrl = getObjectURL(this.files[0]) ;
  if (objUrl) {
            imgShow.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;
}
天蓬老师

这个问题是否是对等于:

建议题主阅读下面的文章来获取解决方案;

  • jquery实现上传图片本地预览效果

  • 用JQuery获取File input里面的数据

应该有相关的jquey插件

这个插件我没有找到,但是当我看到ace的处理方案时候,看到了很大一坨,找到后会补充。

天蓬老师

FileReader API

http://jsfiddle.net/tjqsL5oL/

PHPz

如果你要考虑兼容的话,可以考虑下百度的 http://fex.baidu.com/webuploader/ 已经做了封装了,对于不支持的则用了flash 但是接口保持一致

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号