Jquery文件上载始终失败,文件上载已中止

Sty*_*hon 6 javascript php ajax jquery file-upload

我正在尝试让Blueimp的Jquery文件上传插件在我的网站上运行,但是对于我的生活来说无法上传文件.我整天都在努力工作而且陷入困境.它将上传文件并将其提交给UploadHandler类,但是当它尝试完成该handle_file_upload函数时,它会到达:

file_put_contents(
    $file_path,
    fopen('php://input', 'r'),
    $append_file ? FILE_APPEND : 0
);
Run Code Online (Sandbox Code Playgroud)

但总是返回0.我无法弄清楚文件为什么不上传.我得到的完整回复是:

{"files":[
    {"name":"1397489968-32",
    "size":0,
    "type":"multipart\/form-data; boundary=----WebKitFormBoundaryrmi4L2ouOmB4UTVm",
    "error":"File upload aborted",
    "deleteUrl":"http:\/\/onceridden.demomycms.co.uk\/eshop\/library\/ajax\/?file=1397489968-32",
    "deleteType":"DELETE"}
]}
Run Code Online (Sandbox Code Playgroud)

ajax.file-upload.php 只实例化UploadHandler,没有别的.

如果您想查看UploadHandler的完整代码,可以从github下载,这对我来说太大了.

有人可以告诉我为什么文件不会上传?是的我已经完成了基本操作,例如检查文件夹是CHMOD 777.我已经尝试过各种不同类型的文件(它们必须是图像才能工作,限制为jpg,png或gif)和大小; 都产生相同的结果.

根据要求,这是JS文件:

$(function () {
    'use strict';
    // Change this to the location of your server-side upload handler:
    var url = '/eshop/library/ajax/ajax.file-upload.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();
                });
            });
    $('#register-photo').fileupload({
        url: url,
        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('#register-files');
        $.each(data.files, function (index, file) {
            var node = $('<p/>')
                    .append($('<span/>').text(file.name));
            if (!index) {
                node
                    .append('<br>')
                    .append(uploadButton.clone(true).data(data));
            }
            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);
        $('#register-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');
});
Run Code Online (Sandbox Code Playgroud)

它几乎是你使用插件获得的默认文件,只是更改了ID以匹配我的表单.


更新

经过多次游戏和测试后,我发现当您将输入的名称更改files为其他任何内容时,会出现问题.为什么我不知道.如果你想让它在页面上的多个输入上运行,这显然是一个问题......

我自己创建了一个非常简单的界面版本,这确实允许我更改文件名,因此它必须与他们使用的示例有关.我希望能够使用预览图像等(我在简单的测试中无法弄清楚的)所以我需要解决这个问题.

Sty*_*hon 8

这是为了防止其他人也遇到这个问题.该问题是由paramName选项引起的,如果未设置,则从输入名称获取值.它默认情况下没有设置,因此在更改输入名称时我也改变了paramName,这意味着它不再匹配从UploadHandler类返回的变量.

解决方案是添加paramName: 'files[]'选项.


小智 6

我面临同样的问题(行动中止),并有不同的答案.

我上传文件的文件夹/目录没有上传文件的权限.在Linux服务器上,您可以运行以下命令chmod 777文件当我授予此目录的权限时,我没有收到此错误,并且可以成功上载文件.