Fine Uploader: File is empty

时间秒杀一切 提交于 2019-12-13 02:24:37

问题


i'm trying to integrate "Fine Uploader" into my own CMS. After a successful upload, I remove the uploader to display a link to the file with a delete-button besides it. The problem is, that every upload fails, if the time period after the last upload is too short(less than ~30s). The problem only occurs on web-servers, not on my local one.

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;
});

Debug output:

[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"} 

Server-side Code

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');
    }

}

}


回答1:


Based on the discussion in the comments section, it does seem like you are experiencing some issues server-side. Perhaps something is interfering with the request? I had a similar issue at my workplace at one point. A network appliance was removing the file in the request (or perhaps even more than that, I don't recall) in some cases. The file would succeed on auto-retry though. I suppose its also possible that something isn't quite right in your PHP request handler as well. You will likely need to dig a bit deeper. This may involve inspecting the request as it hits your PHP handler and comparing it with the request according to either Chrome/Firebug's network tab or a proxy (such as Charles). If the requests do not match up, then something is intercepting the request and messing with it. If they do match up, and the request looks ok, then perhaps there is an issue in your PHP code.



来源:https://stackoverflow.com/questions/16276284/fine-uploader-file-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!