文件上传后如何隐藏上传按钮? [英] How do I hide the upload button once the file is uploaded?

查看:775
本文介绍了文件上传后如何隐藏上传按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用blueimp和jquery UI进行文件上传.

文件上传后,我想隐藏此按钮,如果删除了照片,请再次显示该按钮.我该怎么做呢?

这是我的HTML:

<form class="fileupload" action="${pageContext.request.contextPath}/someUrl"
      method="POST" enctype="multipart/form-data">
    <noscript><input type="hidden" name="redirect" value="https://www.somedomain.com"/></noscript>
    <input type="hidden" name="type" value="image1">

    <div class="row fileupload-buttonbar">
        <div class="col-lg-7">
            <span class="btn btn-info fileinput-button"><i class="fa fa-plus"></i> Add one photo...
                <input type="file" name="image" accept="image/png, image/jpeg">
            </span>
            <span class="fileupload-process"></span>
        </div>
        <div class="col-lg-5 fileupload-progress fade">
            <div class="progress progress-striped active" role="progressbar"
                 aria-valuemin="0"
                 aria-valuemax="100">
                <div class="progress-bar progress-bar-success" style="width:0;"></div>
            </div>
            <div class="progress-extended">&nbsp;</div>
        </div>
    </div>
    <table role="presentation" class="table table-striped">
        <tbody class="files"></tbody>
    </table>
</form>

X-TMPL:

<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-upload fade">
        <td>
            <span class="preview"></span>
        </td>
        <td>
            <p class="name">{%=file.name%}</p>
            <strong class="error text-danger"></strong>
        </td>
        <td>
            <p class="size">Processing...</p>
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
        </td>
        <td>
            {% if (!i && !o.options.autoUpload) { %}
                <button class="btn btn-info start" disabled>
                    <span>Start</span>
                    <i class="fa fa-caret-right"></i>
                </button>
            {% } %}
            {% if (!i) { %}
                <button class="btn btn-warning cancel">
                    <i class="fa fa-trash-o"></i>
                    <span>Remove Photo</span>
                </button>
            {% } %}
        </td>
    </tr>
{% } %}
</script>
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-download fade">
        <td>
            <span class="preview">
                {% if (file.thumbnailUrl) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
                {% } else { %}
                    <img src="{%=file.thumbnail_url%}">
                {% } %}
            </span>
        </td>
        <td>
            <p class="name">
                {% if (file.url) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
                {% } else { %}
                    <span>{%=file.name%}</span>
                {% } %}
            </p>
            {% if (file.error) { %}
                <div><span class="text-danger"><i class="fa fa-exclamation-circle"></i> Error</span> {%=file.error%}</div>
            {% } %}
        </td>
        <td>
            <span class="size">{%=o.formatFileSize(file.size)%}</span>
        </td>
        <td>
            {% if (file.deleteUrl) { %}
                <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                    <i class="fa fa-trash-o"></i>
                    <span>Remove Photo</span>
                </button>
                <input type="checkbox" name="delete" value="1" class="toggle">
            {% } else { %}
                <button class="btn btn-warning cancel">
                    <i class="fa fa-trash-o"></i>
                    <span>Remove Photo</span>
                </button>
            {% } %}
        </td>
    </tr>
{% } %}
</script>

在SO上找到的最近的帖子并不完全相关

更新:

尝试了@ZakariaAcharki回答的回调解决方案:

console.log('start')
$('input[name="image"]')
    .bind('fileuploadcompleted', function (e, data) {
        console.log('hiding')
        $('.fileinput-button').hide();
    })
    .bind('fileuploaddestroyed', function (e, data) {
        console.log('showing')
        $('.fileinput-button').show();
    });
console.log('ended')

输出为:开始"和结束".不确定为什么不会被触发.

更新2: 隐藏似乎可以解决此问题,但没有显示.

$('.fileupload')
    .bind('fileuploaddone', function (e, data) {
        console.log('hide');
        $('.fileinput-button').hide();
    })
    .bind('fileuploaddestroy', function (e, data) { //tried fileuploaddestroyed too
        console.log('show');
        $('.fileinput-button').show();
    });

Javascript(除了未更改的tmpl.min.js和jquery文件上载/UI文件):

/*
 * jQuery File Upload Plugin JS Example 8.9.1
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

/* global $, window */

$(function () {
    'use strict';

    var uploadPaths = ["fileA", "fileB", "fileC", "fileCA", "fileCB", "fileCC"];

    // Initialize the jQuery File Upload widget:
    $('.fileupload').each(function (index) {
        $(this).fileupload({
            dropZone: $(this),
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png|doc|docx|pdf|ppt|pptx)$/i,                 maxFileSize: 10000000, // 10 MB

            // Error and info messages:
            messages: {
                acceptFileTypes: 'Sorry, this file type not allowed.  Please make sure the extension of the file is either .gif, .jpg, .jpeg, .png, .doc, .docx, .pdf, .ppt, or .pptx.',  
                maxFileSize: 'Please make sure your file is under 10 MB in size.'
            }
        });

        // Load existing files:
        $(this).addClass('fileupload-processing');
        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: '/' + uploadPaths[index],
            context: $(this)
        }).done(function (data) {
            $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: {files: data.files}});
            $(this).removeClass('fileupload-processing');
        });
    });

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );
});

解决方案

这是我们的开发人员提出的解决方案,它使用CSS隐藏和显示按钮并绑定到fileuploaddone事件.

在具有$('.fileupload').each(function (index)的块中,根据需要进行绑定:

$(this).bind('fileuploaddone', function (e, data){
    var form = $(this).closest("form");
    var uploadButton = form.find(".btn.btn-info.fileinput-button");        
    uploadButton.css("visibility", "hidden");
});

// Load existing files:
if (typeof allFiles[index] !== 'undefined')      
   if (typeof allFiles[index] !== 'undefined'){
      $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: {files: allFiles[index].files}});
      var form = $(this).closest("form");
      var uploadButton = form.find(".btn.btn-info.fileinput-button");
      uploadButton.css("visibility", "hidden");
   }
  });                 
});

,然后在markImageAsDeleted函数中使按钮可见:

var uploadButton = form.find(".btn.btn-info.fileinput-button");
uploadButton.css("visibility", "visible");

I'm using blueimp and jquery UI for file uploads.

I'd like to hide this button once a file is uploaded and display it once again if the photo is removed. How can I go about doing this?

Here is my HTML:

<form class="fileupload" action="${pageContext.request.contextPath}/someUrl"
      method="POST" enctype="multipart/form-data">
    <noscript><input type="hidden" name="redirect" value="https://www.somedomain.com"/></noscript>
    <input type="hidden" name="type" value="image1">

    <div class="row fileupload-buttonbar">
        <div class="col-lg-7">
            <span class="btn btn-info fileinput-button"><i class="fa fa-plus"></i> Add one photo...
                <input type="file" name="image" accept="image/png, image/jpeg">
            </span>
            <span class="fileupload-process"></span>
        </div>
        <div class="col-lg-5 fileupload-progress fade">
            <div class="progress progress-striped active" role="progressbar"
                 aria-valuemin="0"
                 aria-valuemax="100">
                <div class="progress-bar progress-bar-success" style="width:0;"></div>
            </div>
            <div class="progress-extended">&nbsp;</div>
        </div>
    </div>
    <table role="presentation" class="table table-striped">
        <tbody class="files"></tbody>
    </table>
</form>

The X-TMPL:

<!-- The template to display files available for upload -->
<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-upload fade">
        <td>
            <span class="preview"></span>
        </td>
        <td>
            <p class="name">{%=file.name%}</p>
            <strong class="error text-danger"></strong>
        </td>
        <td>
            <p class="size">Processing...</p>
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>
        </td>
        <td>
            {% if (!i && !o.options.autoUpload) { %}
                <button class="btn btn-info start" disabled>
                    <span>Start</span>
                    <i class="fa fa-caret-right"></i>
                </button>
            {% } %}
            {% if (!i) { %}
                <button class="btn btn-warning cancel">
                    <i class="fa fa-trash-o"></i>
                    <span>Remove Photo</span>
                </button>
            {% } %}
        </td>
    </tr>
{% } %}
</script>
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
    <tr class="template-download fade">
        <td>
            <span class="preview">
                {% if (file.thumbnailUrl) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumbnailUrl%}"></a>
                {% } else { %}
                    <img src="{%=file.thumbnail_url%}">
                {% } %}
            </span>
        </td>
        <td>
            <p class="name">
                {% if (file.url) { %}
                    <a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a>
                {% } else { %}
                    <span>{%=file.name%}</span>
                {% } %}
            </p>
            {% if (file.error) { %}
                <div><span class="text-danger"><i class="fa fa-exclamation-circle"></i> Error</span> {%=file.error%}</div>
            {% } %}
        </td>
        <td>
            <span class="size">{%=o.formatFileSize(file.size)%}</span>
        </td>
        <td>
            {% if (file.deleteUrl) { %}
                <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                    <i class="fa fa-trash-o"></i>
                    <span>Remove Photo</span>
                </button>
                <input type="checkbox" name="delete" value="1" class="toggle">
            {% } else { %}
                <button class="btn btn-warning cancel">
                    <i class="fa fa-trash-o"></i>
                    <span>Remove Photo</span>
                </button>
            {% } %}
        </td>
    </tr>
{% } %}
</script>

The closest post I've found on SO was not exactly relevant.

Update:

Tried the callback solution as answered by @ZakariaAcharki:

console.log('start')
$('input[name="image"]')
    .bind('fileuploadcompleted', function (e, data) {
        console.log('hiding')
        $('.fileinput-button').hide();
    })
    .bind('fileuploaddestroyed', function (e, data) {
        console.log('showing')
        $('.fileinput-button').show();
    });
console.log('ended')

Output reads: 'start' and 'ended'. Not sure why it's not getting fired.

Update 2: Hiding seems to work with this, but it doesn't show.

$('.fileupload')
    .bind('fileuploaddone', function (e, data) {
        console.log('hide');
        $('.fileinput-button').hide();
    })
    .bind('fileuploaddestroy', function (e, data) { //tried fileuploaddestroyed too
        console.log('show');
        $('.fileinput-button').show();
    });

The Javascript (aside from the unchanged tmpl.min.js and jquery file upload/UI files):

/*
 * jQuery File Upload Plugin JS Example 8.9.1
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

/* global $, window */

$(function () {
    'use strict';

    var uploadPaths = ["fileA", "fileB", "fileC", "fileCA", "fileCB", "fileCC"];

    // Initialize the jQuery File Upload widget:
    $('.fileupload').each(function (index) {
        $(this).fileupload({
            dropZone: $(this),
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png|doc|docx|pdf|ppt|pptx)$/i,                 maxFileSize: 10000000, // 10 MB

            // Error and info messages:
            messages: {
                acceptFileTypes: 'Sorry, this file type not allowed.  Please make sure the extension of the file is either .gif, .jpg, .jpeg, .png, .doc, .docx, .pdf, .ppt, or .pptx.',  
                maxFileSize: 'Please make sure your file is under 10 MB in size.'
            }
        });

        // Load existing files:
        $(this).addClass('fileupload-processing');
        $.ajax({
            // Uncomment the following to send cross-domain cookies:
            //xhrFields: {withCredentials: true},
            url: '/' + uploadPaths[index],
            context: $(this)
        }).done(function (data) {
            $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: {files: data.files}});
            $(this).removeClass('fileupload-processing');
        });
    });

    // Enable iframe cross-domain access via redirect option:
    $('#fileupload').fileupload(
        'option',
        'redirect',
        window.location.href.replace(
            /\/[^\/]*$/,
            '/cors/result.html?%s'
        )
    );
});

解决方案

Here is the solution our developer presented, by using CSS to hide and show the button, binding to the fileuploaddone event.

In the block with: $('.fileupload').each(function (index), bind as needed:

$(this).bind('fileuploaddone', function (e, data){
    var form = $(this).closest("form");
    var uploadButton = form.find(".btn.btn-info.fileinput-button");        
    uploadButton.css("visibility", "hidden");
});

// Load existing files:
if (typeof allFiles[index] !== 'undefined')      
   if (typeof allFiles[index] !== 'undefined'){
      $(this).fileupload('option', 'done').call(this, $.Event('done'), {result: {files: allFiles[index].files}});
      var form = $(this).closest("form");
      var uploadButton = form.find(".btn.btn-info.fileinput-button");
      uploadButton.css("visibility", "hidden");
   }
  });                 
});

and then later, make the button visible in a markImageAsDeleted function:

var uploadButton = form.find(".btn.btn-info.fileinput-button");
uploadButton.css("visibility", "visible");

这篇关于文件上传后如何隐藏上传按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆