1

我正在尝试将“Fine Uploader”集成到我自己的 CMS 中。成功上传后,我删除了上传程序以显示指向文件的链接,旁边有一个删除按钮。问题是,如果最后一次上传后的时间太短(小于~30s),每次上传都会失败。这个问题只发生在网络服务器上,而不是我的本地服务器上。

uploader = $('.file-uploader').fineUploader({
    request: {
        endpoint: fullpath+'/detailseite/upload/tmp'
    },
    validation: {
        allowedExtensions: extensions
    },
    retry: {
        enableAuto: true
    },
    text: {
        uploadButton: '<div class="btn button-white">File hochladen</div>'
    },
    multiple: false
}).on('progress', function (id, fileName, uploadedBytes, totalBytes) {
    $('.new_elements:not(.disabled),.update_elements:not(.disabled)').addClass('disabled');

}).on('complete', function(event, id, fileName, responseJSON) {
    if (responseJSON.success){
        //uploader.fineUploader('reset');
        file_name = responseJSON.name;
        $('.file-uploader').html('<a target="_blank" href="<?php echo PATH; ?>files/tmp/'+file_name+'" ><b>'+file_name+'</b></a><input type="hidden" name="name" value="'+file_name+'" />');
        $('.file-uploader').append('<a href="<?php echo $full_path; ?>/contentmanagement/file_loeschen/tmp/'+file_name+'/dokument" class="delete_imageu"></a>');
        $('.new_elements,.update_elements').removeClass('disabled');
    }
});

$(document).off('click','.'+element_id+' a.delete_imageu');
$(document).on('click','.'+element_id+' a.delete_imageu', function()
{
    var url = $(this).attr('href');
    $.ajax({
        type: "POST",
        url: url,
        success: function() {
            $('.'+element_id).children().remove();
            $('.'+element_id).html('<div class="file-uploader" id="<?php echo $element_id; ?>"></div>');
            name = element_id;
            //uploader.fineUploader('reset');
            uploader = $('.'+element_id+' .file-uploader').fineUploader({
                request: {
                    endpoint: fullpath+'/detailseite/upload/tmp'
                },
                validation: {
                    allowedExtensions: extensions
                },
                retry: {
                    enableAuto: true
                },
                text: {
                    uploadButton: '<div class="btn button-white">File hochladen</div>'
                },
                debug: true,
                multiple: false
            }).on('progress', function (id, fileName, uploadedBytes, totalBytes) {
                $('.new_elements:not(.disabled),.update_elements:not(.disabled)').addClass('disabled');

            }).on('complete', function(event, id, fileName, responseJSON){
                if (responseJSON.success){
                    file_name = responseJSON.name;
                    $(this).html('<a target="_blank" href="<?php echo PATH; ?>files/tmp/'+file_name+'" ><b>'+file_name+'</b></a><input type="hidden" name="name" value="'+file_name+'" />');
                    $(this).append('<a href="<?php echo $full_path; ?>/contentmanagement/file_loeschen/tmp/'+file_name+'/dokument" class="delete_imageu"></a>');
                    $('.new_elements,.update_elements').removeClass('disabled');
                }
            });
        }
    });
    return false;
});

调试输出:

[FineUploader] Processing 1 files or inputs... jquery.fineuploader-3.5.0.min.js:4
[FineUploader] Sending upload request for 1 jquery.fineuploader-3.5.0.min.js:4
[FineUploader] xhr - server response received for 1 jquery.fineuploader-3.5.0.min.js:4
[FineUploader] responseText = {"error":"File is empty"} jquery.fineuploader-3.5.0.min.js:4
[FineUploader] Waiting 5 seconds before retrying zztailtoddle_lo.mp3... jquery.fineuploader-3.5.0.min.js:4
[FineUploader] Retrying zztailtoddle_lo.mp3... jquery.fineuploader-3.5.0.min.js:4
[FineUploader] Sending upload request for 1 jquery.fineuploader-3.5.0.min.js:4
[FineUploader] xhr - server response received for 1 jquery.fineuploader-3.5.0.min.js:4
[FineUploader] responseText = {"success":true,"name":"zztailtoddle_lo38.mp3","type":"mp3"} 

服务器端代码

class qqFileUploader {
private $allowedExtensions = array();
private $sizeLimit = 10485760;
private $file;

function __construct(array $allowedExtensions = array(), $sizeLimit = 10485760){
    $allowedExtensions = array_map("strtolower", $allowedExtensions);

    $this->allowedExtensions = $allowedExtensions;
    $this->sizeLimit = $sizeLimit;

    $this->checkServerSettings();

    if (isset($_GET['qqfile'])) {
        $this->file = new qqUploadedFileXhr();
    } elseif (isset($_FILES['qqfile'])) {
        $this->file = new qqUploadedFileForm();
    } else {
        $this->file = false;
    }
}

private function checkServerSettings(){
    $postSize = $this->toBytes(ini_get('post_max_size'));
    $uploadSize = $this->toBytes(ini_get('upload_max_filesize'));

    if ($postSize < $this->sizeLimit || $uploadSize < $this->sizeLimit){
        $size = max(1, $this->sizeLimit / 1024 / 1024) . 'M';
        die("{'error':'increase post_max_size and upload_max_filesize to $size'}");
    }
}

private function toBytes($str){
    $val = trim($str);
    $last = strtolower($str[strlen($str)-1]);
    switch($last) {
        case 'g': $val *= 1024;
        case 'm': $val *= 1024;
        case 'k': $val *= 1024;
    }
    return $val;
}

/**
 * Returns array('success'=>true) or array('error'=>'error message')
 */
function handleUpload($uploadDirectory, $replaceOldFile = false,$thumbnail = false,$width = null,$height = null){

    if (!$this->file){
        return array('error' => 'No files were uploaded.');
    }

    $size = $this->file->getSize();

    if ($size == 0) {
        return array('error' => 'File is empty');
    }

    if ($size > $this->sizeLimit) {
        return array('error' => 'File is too large');
    }

    $pathinfo = pathinfo($this->file->getName());
    $filename = normalize_string($pathinfo['filename']);

    $ext = $pathinfo['extension'];

    if($this->allowedExtensions && !in_array(strtolower($ext), $this->allowedExtensions)){
        $these = implode(', ', $this->allowedExtensions);
        return array('error' => 'File has an invalid extension, it should be one of '. $these . '.');
    }

    if(!$replaceOldFile){
        /// don't overwrite previous files that were uploaded
        while (file_exists($uploadDirectory . $filename . '.' . $ext)) {
            $filename .= rand(10, 99);
        }
    }

       if ($this->file->save($uploadDirectory.$filename.'.'.$ext)){

       $src =  $uploadDirectory.$filename.'.' . $ext;


        return array('success'=> true,'name' => $filename. '.' .$ext,'type' => $ext);
    } else {
        return array('error'=> 'Could not save uploaded file.' .
            'The upload was cancelled, or server error encountered');
    }

}

}

4

1 回答 1

1

根据评论部分的讨论,您似乎确实在服务器端遇到了一些问题。也许有什么东西干扰了请求?我曾经在工作场所遇到过类似的问题。在某些情况下,网络设备正在删除请求中的文件(或者甚至更多,我不记得了)。该文件会在自动重试时成功。我想您的 PHP 请求处理程序中的某些内容也可能不太正确。您可能需要更深入地挖掘。这可能涉及在请求到达您的 PHP 处理程序时检查请求,并根据 Chrome/Firebug 的网络选项卡或代理(例如 Charles)将其与请求进行比较。如果请求不匹配,那么某些东西正在拦截请求并弄乱它。如果他们真的匹配,

于 2013-04-29T13:46:18.033 回答