简体   繁体   English

使用Blueimp jQuery-File-Upload与wordpress和联系表单7文件上传

[英]using Blueimp jQuery-File-Upload with wordpress and contact form 7 file upload

I am using wordpress 4.8, contactform7 4.8 and jquery file upload 我正在使用wordpress 4.8, contactform7 4.8jquery文件上传

and trying to add multiple file upload support in contact form 7. 并尝试在联系表单7中添加多个文件上载支持。

now, my script is 现在,我的剧本是

( function( $ ) {
    'use strict';
    var url = window.location.hostname === 'blueimp.github.io' ?
              '//jquery-file-upload.appspot.com/' : ajax_object.ajax_url;

    $('.wpcf7-multifile').fileupload({
        url: url,
        dataType: 'json',
        autoUpload: true,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 99900000,
        // 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) {
        //console.log(data.files);
        data.context = $('<div/>').appendTo('#files');
        data.formData = {action : 'do_file_upload'};
        $.each(data.files, function (index, file) {
            var node = $('<p/>')
                    .append($('<span/>').text(file.name));
            if (!index) {
                node
                    .append('<br>')
            }
            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));
        }
    }).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) {
        console.log(data.result.files);
        $.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) {
        //console.log("upload failed");
        console.log(e);
        $.each(data.files, function (index) {
            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');

} )( jQuery );

and this is my UploadHandler.php . 这是我的UploadHandler.php

my html is 我的HTML是

<p><label> Multi File<br>
<span class="wpcf7-form-control-wrap multifile-966"><input type="file" name="multifile-966[]" size="40" class="wpcf7-form-control wpcf7-multifile multi-file" id="multi-file" multiple="multiple" aria-invalid="false"></span></label></p>

server site ajax callback is 服务器站点ajax回调是

add_action('wp_ajax_do_file_upload', 'do_file_upload');
add_action('wp_ajax_nopriv_do_file_upload', 'do_file_upload');

if ( ! function_exists('do_file_upload') ){
    function do_file_upload(){
        $options = [
        'param_name' => key($_FILES)
    ];
    require('server/php/UploadHandler.php');
    $upload_handler = new UploadHandler($options);
    }
}

file upload getting failed. 文件上传失败。

console log for e in fileuploadfail event fileuploadfail事件中的e控制台日志

n.Event {type: "fileuploadfail", timeStamp: 1500289969495, jQuery112406601460352960828: true, target: input#multi-file.wpcf7-form-control.wpcf7-multifile.multi-file, isTrigger: 3…}

network response {"multifile-966":[{"name":"Beautiful-Nature-Images-Wallpaper-6755.jpg","size":565592,"type":"image\\/jpeg","url":"http:\\/\\/localhost\\/contactform7\\/wp-content\\/uploads\\/wpcf7_uploads\\/Beautiful-Nature-Images-Wallpaper-6755.jpg","thumbnailUrl":"http:\\/\\/localhost\\/contactform7\\/wp-content\\/uploads\\/wpcf7_uploads\\/thumbnail\\/Beautiful-Nature-Images-Wallpaper-6755.jpg","deleteUrl":"http:\\/\\/localhost\\/contactform7\\/wp-admin\\/admin-ajax.php?multifile-96=Beautiful-Nature-Images-Wallpaper-6755.jpg","deleteType":"DELETE"}]}0 网络响应{"multifile-966":[{"name":"Beautiful-Nature-Images-Wallpaper-6755.jpg","size":565592,"type":"image\\/jpeg","url":"http:\\/\\/localhost\\/contactform7\\/wp-content\\/uploads\\/wpcf7_uploads\\/Beautiful-Nature-Images-Wallpaper-6755.jpg","thumbnailUrl":"http:\\/\\/localhost\\/contactform7\\/wp-content\\/uploads\\/wpcf7_uploads\\/thumbnail\\/Beautiful-Nature-Images-Wallpaper-6755.jpg","deleteUrl":"http:\\/\\/localhost\\/contactform7\\/wp-admin\\/admin-ajax.php?multifile-96=Beautiful-Nature-Images-Wallpaper-6755.jpg","deleteType":"DELETE"}]}0

expected network dummy response 预期的网络虚拟响应

{files: [,…]}
files
:
[,…]
0
:
{name: "46541-purple-sunrise-over-the-mountains-1920x1200-nature-wallpaper (1).jpg", size: 566874,…}

I converted these two response to array in UploadHandler.php file post method and both are 100% same !!only difference is a 0 at the end of the json response, may be that is giving some idea, but now I have no idea why not it is showing file upload failed, the file are getting uploaded but temporarily when I am refreshing the page, they disappeared . 我在UploadHandler.php文件post方法中将这两个响应转换为数组,并且两者都是100%相同!!只是在json响应结束时差异为0,可能是给出了一些想法,但现在我不知道为什么不是它显示文件上传失败,文件正在上传但暂时当我刷新页面时,它们消失了。

also updated UploadHandler.php . 还更新了UploadHandler.php。

Update 更新

changed

return $this->generate_response($response, $print_response);

to

$print_response = false;
    wp_send_json($this->generate_response($response, $print_response));

makes the extra 0 disappeared, but still the upload is temporary, but expected behavior is upload to be permanent. 使额外的0消失,但上传仍然是暂时的,但预期的行为上传是永久性的。

update2 UPDATE2

in UploadHandler.php for 在UploadHandler.php中

move_uploaded_file($uploaded_file, $file_path);

I am getting true. 我变成了现实。 echo var_dump(move_uploaded_file($uploaded_file, $file_path)); is true . true

changing UploadHandler.php 改变UploadHandler.php

these two lines 这两行

 'upload_dir' => dirname(dirname($this->get_server_var('SCRIPT_FILENAME'))) . '/wp-content/uploads/wpcf7_uploads/',
            'upload_url' => $wp_content_url . 'wpcf7_uploads/',

to any other folder beside wpcf7_uploads makes it work. 到wpcf7_uploads旁边的任何其他文件夹使它工作。

I changed it to 我改成了

'upload_dir' => dirname(dirname($this->get_server_var('SCRIPT_FILENAME'))) . '/wp-content/uploads/2017/wpcf7_uploads/',
            'upload_url' => $wp_content_url . '2017/wpcf7_uploads/',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM