【问题标题】:Preview the uploaded image in product page in Opencart在 Opencart 的产品页面中预览上传的图片
【发布时间】:2016-01-25 04:53:37
【问题描述】:

我正在使用 Opencart 版本 2.0.3.1

客户可以在产品页面上传image (upload file option)。我想在同一页面中preview the uploaded image。上传的图片保存到system/upload文件夹并用文件名+一些字符串重命名。

如何预览上传的图片。

文件上传按钮代码为:(catalog/view/theme/default/template/product.product.tpl)

    <?php if ($option['type'] == 'file') { ?>
    <div class="form-group<?php echo ($option['required'] ? ' required' : ''); ?>">
      <label class="control-label"><?php echo $option['name']; ?></label>
      <button type="button" id="button-upload<?php echo $option['product_option_id']; ?>" data-loading-text="<?php echo $text_loading; ?>" class="btn btn-default btn-block"><i class="fa fa-upload"></i> <?php echo $button_upload; ?></button>
      <input type="hidden" name="option[<?php echo $option['product_option_id']; ?>]" value="" id="input-option<?php echo $option['product_option_id']; ?>" />
    </div>
    <?php } ?>

【问题讨论】:

    标签: php jquery file-upload opencart2.x


    【解决方案1】:

    不是最佳解决方案,但您可以尝试这个。

    添加

    &lt;img id="filePreview" style="display:none;"&gt;

    之后

    &lt;?php if ($option['type'] == 'file') { ?&gt;

    添加

    var ref = '';
    
    $(document).on('change','input[name="file"]',function(){
        ref = this;
    });
    

    之前

    $('button[id^=\'button-upload\']').on('click', function() {

    在文件上传的ajax请求中,在成功函数中添加此项

    if (json['error']) {
            ref = '';
            $(node).parent().find('input').after('<div class="text-danger">' + json['error'] + '</div>');
    }
    
    if (json['success']) {
        alert(json['success']);
        showImagePreview(ref);
        $(node).parent().find('input').attr('value', json['code']);
    }
    

    节目预览功能是

    function showImagePreview(input){
    
     if (input.files && input.files[0]) {
        var reader = new FileReader();
        reader.onload = function (e) {
            $('#filePreview').attr('src',e.target.result);
            $('#filePreview').show();
        }
        reader.readAsDataURL(input.files[0]);
     }
    }
    

    希望它会起作用:)

    【讨论】:

    • 很高兴它帮助了你:)
    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多