如何在没有选择文件的情况下使JQuery文件上传插件工作?

时间:2015-08-12 22:54:50

标签: javascript html sails.js jquery-file-upload jquery-fileupload-rails

我知道这个问题已被提出before并且没有给出令人满意的答案。我使用sails.js开发网站。

我有一个包含一些文本字段和文件上传选项的表单(我已经使用了JQuery文件上传插件)。但是,如果未选择任何文件且仅输入文本字段,则提交不会继续进行。

这是我的main.js提交功能:

$(function () {
    'use strict';
var propertyId = document.getElementById("helper").getAttribute("data-name");
var fileCount = 0, fails = 0, successes = 0;
    // Initialize the jQuery File Upload widget:
    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        //url: 'server/php/'
        singleFileUploads: false,
        url: '/admin/property/'+propertyId+'/pictures/upload'
    })

这是我的表格:

<form id="fileupload" action="/admin/property/<%=propertyId%>/pictures/upload" method="POST" enctype="multipart/form-data">
        <noscript><input type="hidden" name="redirect" value="https://blueimp.github.io/jQuery-File-Upload/"></noscript>
        <div class="row fileupload-buttonbar">
            <div class="col-lg-7">
                <label>Stage Title:</label>
                    <input class="form-control" type="text"  name="stageTitle"/>
                    <br>
                <label>Start Date:</label>
                    <input class="form-control" type="text" name="startDate"/>
                    <br>
                <label>End Date:</label>
                    <input class="form-control" type="text" name="endDate"/>
                    <br>
                <label>Description:</label>
                    <textarea class="form-control" type="text" name="description"> </textarea>
                    <br>
                <label>Pictures:</label><br>
                    <span class="btn btn-success fileinput-button">
                        <span>Add Images...</span>
                        <input type="file" name="files" id="files" multiple>
                    </span>

                    <button type="reset" class="btn btn-warning cancel">
                        <span>Cancel upload</span>
                    </button>

            <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>
            <button type="submit" class="btn btn-primary start">
                    <span>Save</span>
        </div>

        <table id="pictureTable" role="presentation" class="table table-striped"><tbody class="files"></tbody></table>

            </button>
    </form>

如何在没有选择文件的情况下使库工作? 其他选项是制作两个单独的提交,一个用于文本字段,一个用于文件。但它会非常混乱,我不喜欢这样。任何指导将不胜感激。

1 个答案:

答案 0 :(得分:1)

只需检查文件是否已设置:

if($('#fileupload').val() != ""){
    $('#fileupload').fileupload({
        // Uncomment the following to send cross-domain cookies:
        //xhrFields: {withCredentials: true},
        //url: 'server/php/'
        singleFileUploads: false,
        url: '/admin/property/'+propertyId+'/pictures/upload'
    });
}