简体   繁体   English

jQuery文件上传预览/删除图像

[英]jQuery file upload preview/remove image

You can find the working file at jsfiddle 您可以在jsfiddle中找到工作文件

Image preview and remove function was working early, But I did some changes in the html code because of some libraries. 图像预览和删除功能很早就开始起作用,但是由于某些库,我在html代码中做了一些更改。 So now image preview is not working because of parent class calling issue in Jquery. 因此,由于Jquery中的父类调用问题,图像预览现在无法正常工作。 Please check the working code and help me to solve it. 请检查工作代码并帮助我解决。 Im using multiple input with same name array. 我使用具有相同名称数组的多个输入。 So I want to display the image in next div as I mentioned in the html code. 所以我想在html代码中提到的下一个div中显示图像。 I knew the problem is from jquery. 我知道问题出在jquery。 But I couldn't fix it. 但是我无法解决。 Please help. 请帮忙。

function readURL() {
     var $input = $(this);
     if (this.files && this.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
           reset($input.next('.delbtn'), true);
           $input.next('.portimg').attr('src', e.target.result).show();
           $input.after('<input type="button" class="delbtn removebtn" value="remove">');
        }
           reader.readAsDataURL(this.files[0]);
      }
    }
    $(".fileUpload").change(readURL);
    $("form").on('click', '.delbtn', function (e) {
        reset($(this));
    });

function reset(elm, prserveFileName) {
    if (elm && elm.length > 0) {
       var $input = elm;
       $input.next('.portimg').attr('src', '').hide();
       if (!prserveFileName) {
         $input.prev('.fileUpload').val("");
       }
         elm.remove();
       }
}

html code HTML代码

<form> <!--working code but it not needed now.-->
  <div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic type=file value="Choose a file"> 
        <img alt="your image" class=portimg src=#>
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div> 
<!--below code is not working. I need this to be work-->
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>

Please try this version: 请尝试以下版本:

HTML: HTML:

<form>
<div>
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic[] type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>
</div>

<div>
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic[] type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>
</div>

<div>
<div class="form-group hirehide is-empty is-fileinput width100">
    <div class=socialmediaside2>
        <input class=fileUpload accept="image/jpeg, image/jpg" name=profilepic[] type=file value="Choose a file">
        <div class=input-group>
            <input class=form-control id=uploadre placeholder="Please select your profile picture" readonly>
            <span class="input-group-btn input-group-sm"><button class="btn btn-fab btn-fab-mini"type=button><i class=material-icons>attach_file</i></button></span>
        </div>
    </div>
</div>
<div class=upload-demo>
    <div class=upload-demo-wrap><img alt="your image" class=portimg src=#></div>
</div>
</div>

</form>

JS: JS:

function readURL() {
    var $input = $(this);
    var $newinput =  $(this).parent().parent().parent().find('.portimg ');
    if (this.files && this.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            reset($newinput.next('.delbtn'), true);
            $newinput.attr('src', e.target.result).show();
            $newinput.after('<input type="button" class="delbtn removebtn" value="remove">');
        }
        reader.readAsDataURL(this.files[0]);
    }
}
$(".fileUpload").change(readURL);
$("form").on('click', '.delbtn', function (e) {
    reset($(this));
});

function reset(elm, prserveFileName) {
    if (elm && elm.length > 0) {
        var $input = elm;
        $input.prev('.portimg').attr('src', '').hide();
        if (!prserveFileName) {
            $($input).parent().parent().parent().find('input.fileUpload ').val("");
            //input.fileUpload and input#uploadre both need to empty values for particular div
        }
        elm.remove();
    }
}

JSFiddle: https://jsfiddle.net/hxwmL1px/4/ JSFiddle: https ://jsfiddle.net/hxwmL1px/4/

Just add the below html code. 只需添加以下html代码。 Check Working Demo now check updated link below in comments. 现在检查工作演示,检查下面注释中的更新链接。

 <img alt="your image" class=portimg src=#>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM