首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$_FILES数组空

$_FILES数组空
EN

Stack Overflow用户
提问于 2013-12-31 20:37:32
回答 3查看 6.2K关注 0票数 8

使用Jquery文件上传。这是“工作”和上传图像&显示拇指。但是,当我在处理程序中提交表单时,如果我转储$_FILES,就什么都没有了。

我基本上是在使用基本加号示例,其中autoUpload设置为false。我希望我能用这个让用户选择多个图像。然后让他们上传一旦表单提交,并处理基本上相同的方式,我会处理他们,如果我有X个文件上传按钮在一个页面上。

使用autoUpload=TRUE上传它们也同样有效。我试过了,也没有在帖子或文件中看到任何东西。

让这两种方法发挥作用的注释都是很好的。

下面是我的js。

代码语言:javascript
复制
$('#fileupload').fileupload({
    url: url,
    method: 'POST',
    dataType: 'json',
    autoUpload: false,
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    maxFileSize: 5000000, // 5 MB
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator.userAgent),
    previewMaxWidth: 100,
    previewMaxHeight: 100,
    previewCrop: true
}).on('fileuploadadd', function (e, data) {
    data.context = $('<div/>').appendTo('#files');
    $.each(data.files, function (index, file) {
        var node = $('<p/>')
                .append($('<span/>').text(file.name));
        node.appendTo(data.context);
    });
}).on('fileuploadprocessalways', function (e, data) {
    var index = data.index,
        file = data.files[index],
        node = $(data.context.children()[index]);
    if (file.preview) {
        node
            .prepend('<br>')
            .prepend(file.preview);
    }
    if (file.error) {
        node
            .append('<br>')
            .append($('<span class="text-danger"/>').text(file.error));
    }
    if (index + 1 === data.files.length) {
        data.context.find('button')
            .text('Upload')
            .prop('disabled', !!data.files.error);
    }
}).on('fileuploadprogressall', function (e, data) {
    var progress = parseInt(data.loaded / data.total * 100, 10);
    $('#progress .progress-bar').css(
        'width',
        progress + '%'
    );
}).on('fileuploaddone', function (e, data) {
    $.each(data.result.files, function (index, file) {
        if (file.url) {
            var link = $('<a>')
                .attr('target', '_blank')
                .prop('href', file.url);
            $(data.context.children()[index])
                .wrap(link);
        } else if (file.error) {
            var error = $('<span class="text-danger"/>').text(file.error);
            $(data.context.children()[index])
                .append('<br>')
                .append(error);
        }
    });
}).on('fileuploadfail', function (e, data) {
    $.each(data.files, function (index, file) {
        var error = $('<span class="text-danger"/>').text('File upload failed.');
        $(data.context.children()[index])
            .append('<br>')
            .append(error);
    });
}).prop('disabled', !$.support.fileInput)
    .parent().addClass($.support.fileInput ? undefined : 'disabled');

这是我的html

代码语言:javascript
复制
<form action="/submit_form" accept-charset="utf-8" class="form-horizontal review-validate-form" id="review-form" autocomplete="off" enctype="multipart/form-data" method="POST"><div style="display:none">
代码语言:javascript
复制
        <div class="control-group">
            <label class="required control-label" for="first_name">Comments <span class="required">*</span></label>
            <div class="controls">
                <textarea name="comments" cols="40" rows="10" class="span8 required" id="comments" ></textarea>             </div>
        </div>



        <!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
    <i class="glyphicon glyphicon-plus"></i>
    <span>Add files...</span>
    <!-- The file input field used as target for the file upload widget -->
    <input id="fileupload" type="file" name="files[]" multiple>
</span>
<br>
<br>
<!-- The global progress bar -->
<div id="progress" class="progress">
    <div class="progress-bar progress-bar-success"></div>
</div>
<!-- The container for the uploaded files -->
<div id="files" class="files"></div>
<br>



        <div class="form-actions" style="">
            <input type="submit" value="Submit Review" name="submitReview" class="btn btn-primary btn-large">
        </div>
        </form>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-01-10 11:09:57

您需要设置正确的数据类型dataType:' json ',对于多部分形式的数据,您不能使用数据类型json属性设置$_FILES变量。

票数 3
EN

Stack Overflow用户

发布于 2013-12-31 21:40:07

通过为上传的每个文件附加一个隐藏的输入,我可以处理它们,并在表单提交后将它们添加到数据库中。

我觉得应该有一些内置的东西来处理这件事,但就目前而言,这是可行的。

添加了一个filesHidden div来保存隐藏字段。

代码语言:javascript
复制
<div id="files" class="files"></div>

然后更新带有文件名的隐藏输入的js,以传递给我的表单处理程序,这样我就可以将图像与表单提交连接起来。

代码语言:javascript
复制
}).on('fileuploaddone', function (e, data) {
        $.each(data.result.files, function (index, file) {
            if (file.url) {
                $( "#filesHidden" ).append( '<input type="hidden" name="images[]" value="' + file.name + '">' );
            } else if (file.error) {
                var error = $('<span class="text-danger"/>').text(file.error);
                $(data.context.children()[index])
                    .append('<br>')
                    .append(error);
            }
    });
        }

下面是完整的例子(请参阅下面的评论)。注意,我的示例还向每个上传的图像添加了标题文本框。

代码语言:javascript
复制
var url = '/js/fileUpload/server/php/';
uploadButton = $('<button/>')
 .addClass('btn btn-primary')
 .prop('disabled', true)
 .text('Processing...')
 .on('click', function () {
     var $this = $(this),
         data = $this.data();
     $this
         .off('click')
         .text('Abort')
         .on('click', function () {
             $this.remove();
             data.abort();
         });
     data.submit().always(function () {
         $this.remove();
     });
 });

$('#fileupload').fileupload({
    url: url,
    method: 'POST',
    dataType: 'json',
    autoUpload: true,
    acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
    maxFileSize: 5000000, // 5 MB
    // Enable image resizing, except for Android and Opera,
    // which actually support image resizing, but fail to
    // send Blob objects via XHR requests:
    disableImageResize: /Android(?!.*Chrome)|Opera/
        .test(window.navigator.userAgent),
    previewMaxWidth: 100,
    previewMaxHeight: 100,
    previewCrop: true
}).on('fileuploadadd', function (e, data) {
    data.context = $('<div/>').appendTo('#files');
}).on('fileuploadprocessalways', function (e, data) {
    var index = data.index,
        file = data.files[index],
        node = $(data.context.children()[index]);
    if (file.preview) {
        var node = $('<p/>')
        .append('<br /><strong>Description</strong>: <input type="text" name="title[]" value="">');
        node.appendTo(data.context);

        node = $(data.context.children()[index]);
        node
            .prepend('<br>')
            .prepend(file.preview);
    }
    if (file.error) {
        alert(file.error);
    }
    if (index + 1 === data.files.length) {
        data.context.find('button')
            .text('Upload')
            .prop('disabled', !!data.files.error);
    }
}).on('fileuploadprogressall', function (e, data) {
    var progress = parseInt(data.loaded / data.total * 100, 10);
    $('#progress .progress-bar').css(
        'width',
        progress + '%'
    );
}).on('fileuploaddone', function (e, data) {
    $.each(data.result.files, function (index, file) {
        if (file.url) {
            $( "#filesHidden" ).append( '<input type="hidden" name="images[]" value="' + file.name + '">' );
        } else if (file.error) {
            var error = $('<span class="text-danger"/>').text(file.error);
            $(data.context.children()[index])
                .append('<br>')
                .append(error);
        }
    });
}).on('fileuploadfail', function (e, data) {
    $.each(data.files, function (index, file) {
        var error = $('<span class="text-danger"/>').text('File upload failed.');
        $(data.context.children()[index])
            .append('<br>')
            .append(error);
    });
}).prop('disabled', !$.support.fileInput)
    .parent().addClass($.support.fileInput ? undefined : 'disabled');
票数 3
EN

Stack Overflow用户

发布于 2014-10-30 12:23:20

我之前也遇到过类似的问题,我的解决方案是在php.ini中增加post数据大小数据和每个文件的最大大小:

代码语言:javascript
复制
post_max_size=15M
upload_max_filesize=15M
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20862937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档