我正在尝试将“Fine Uploader”集成到我自己的CMS中。成功上传后,我删除上传器以显示文件的链接,除此之外还有一个删除按钮。问题是,如果上次上传后的时间段太短(小于~30秒),则每次上传都会失败。该问题仅发生在Web服务器上,而不是本地服务器上。
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');
}
}
}
答案 0 :(得分:1)
根据评论部分中的讨论,您似乎遇到了服务器端的一些问题。也许某些事情干扰了这个请求?我曾经在我的工作场所遇到过类似的问题。在某些情况下,网络设备正在删除请求中的文件(或者甚至更多,我不记得)。该文件将在自动重试时成功。我想也可能在你的PHP请求处理程序中也不太正确。你可能需要深入挖掘一下。这可能涉及在请求到达PHP处理程序时检查请求,并根据Chrome / Firebug的网络选项卡或代理(例如Charles)将其与请求进行比较。如果请求不匹配,那么有些东西正在拦截请求并弄乱它。如果它们匹配,并且请求看起来没问题,则可能是PHP代码中存在问题。