问题描述
我正在尝试将精细上传器"集成到我自己的CMS中.成功上传后,我将删除上传器,以显示指向该文件的链接,并带有一个删除按钮.问题是,如果上次上传后的时间间隔太短(少于30秒),则每次上传都会失败.该问题仅出现在Web服务器上,而不是我的本地服务器上.
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;
});
调试输出:
[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');
}
}
}
推荐答案
基于评论部分的讨论,似乎您在服务器端遇到了一些问题.也许有什么干扰请求?我曾经在工作场所遇到过类似的问题.在某些情况下,网络设备正在删除请求中的文件(甚至可能更多,我不记得了).该文件将自动重试成功.我想也有可能在您的PHP请求处理程序中也不太正确.您可能需要更深入地研究.这可能涉及检查请求是否到达您的PHP处理程序,并根据Chrome/Firebug的网络"标签或代理(例如Charles)将其与请求进行比较.如果请求不匹配,则表示正在拦截该请求并将其弄乱.如果它们确实匹配,并且请求看起来正常,则说明您的PHP代码中有问题.
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.
这篇关于精细上传器:文件为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!