Dropzone.js 和保管箱 API
Dropzone.js and dropbox API
我正在我的系统中实现 dropzone.js 根据文件的大小将文件上传到服务器或保管箱。
而且我还使用甜蜜提醒来通知。
当文件上传到我的服务器时 dropzone 工作正常 但是当它上传到 dropbox 时不return 任何结果
使用大于 6 Mb 的文件进行测试
并且加载栏无法正常工作
HTML:
<form id="cuadro" action="" class="dropzone">
</form>
<p id="texto_carga" style="color: #009688; display:none">Espera mientras se procesa el archivo...</p>
JS:
<script type="text/javascript">
var errors = false;
var Dropzone = new Dropzone("#cuadro", {
url: "../utilidades/pruebasupload.php?id=<?=$personaId?>&codigo=<?=$codigo?>",
acceptedFiles: ".EDF,.edf,.pdf,.PDF,.rar,.RAR,.jpg,.png,.gif",
maxFiles: 1,
error:function(){
errors = true;
},
processing:function(){
$('#texto_carga').show();
},
complete:function(){
if(errors){
swal({
title: 'Error al cargar el achivo!',
text: 'Ha ocurrido un error al intentar cargar el archivo. Póngase en contacto con el administrador del sistema',
type: 'error',
icon: 'error'
});
$('#texto_carga').hide();
}else{
swal({
title: 'Carga completa!',
text: 'Hemos cargado el archivo de la prueba exitosamente',
type: 'success',
icon: 'success'
});
$('#texto_carga').hide();
}
}
});
</script>
PHP pruebasupload.php
require_once "../terceros/dropbox/vendor/autoload.php";
require_once '../clases/servidor_archivos_controller.php';
require_once '../clases/conexion.php';
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
$conexion = new conexion;
$_servidor = new servidorArchivos;
$data = $_servidor->buscarConexion();
$dropboxKey = "";
$dropboxSecret = "";
$acessToken = "";
$appName= "";
$megas = "";
if(empty($data)){
$megas = 200000;
}else{
$dropboxKey = $data[0]['Keyapp'];
$dropboxSecret = $data[0]['Secret'];
$acessToken = $data[0]['Token'];
$appName= $data[0]['Appname'];
$megas = $data[0]['Megas'];
$megas = ($megas * 1024) * 1024 ;
}
if($tama[0]>$megas){
try{
//upload file to dropbox
$file = $dropbox->simpleUpload($tempFile,$nombredropbox, ['autorename' => true]);
//share a file
$response = $dropbox->postToAPI("/sharing/create_shared_link_with_settings", ["path" => $nombredropbox, "settings" => ['requested_visibility' => 'public']]);
$data = $response->getDecodedBody();
$link = $data['url'];
//save link to document in to DB
$query = "insert into pruebas_archivos (Codigo,Archivo,Ubicacion)values('$savecodge','$nombredropbox','2')";
$datos= $conexion->NonQuery($query);
http_response_code(200);
}catch(\EXCEPTION $e){
ERROR('001',$E);
http_response_code(400);
}
}else{
$targetPath = "../public/pruebas/";
$targetFile = $targetPath.$id ."_". $nombreCompleto; //5
move_uploaded_file($tempFile,$targetFile);
//save the url into DB
$query = "insert into pruebas_archivos (Codigo,Archivo,Ubicacion)values('$savecodge','$nombreCompleto','1')";
$conexion->NonQuery($query);
http_response_code(200);
}
}
function error($numero,$texto){
$ddf = fopen('error.log','a');
fwrite($ddf,"[".date("r")."] Error $numero: $texto\r\n");
fclose($ddf);
}
PHP 初始化文件
我已经测试了您的演示,有时能够上传大于 6mb 的文件。
有时,在上传时,脚本会在 30 秒时死掉。
因此,看起来你的代码逻辑没有问题,但是你的服务器出现了一些超时。正如您已经指出的那样,max_execution_time
和 max_input_time
似乎设置正确(我会用 phpinfo();
仔细检查),但取决于您是 运行 的服务器,您可能还有其他地方需要检查。
- 如果在 Apache 上是 运行,请检查
httpd.conf
和 TimeOut
指令。
- 如果您使用的是
php-fpm
,请检查 www.conf
和 request_terminate_timeout
指令。
- 如果您使用的是 nginx,请检查
fastcgi_read_timeout
指令
我正在我的系统中实现 dropzone.js 根据文件的大小将文件上传到服务器或保管箱。
而且我还使用甜蜜提醒来通知。
当文件上传到我的服务器时 dropzone 工作正常 但是当它上传到 dropbox 时不return 任何结果
使用大于 6 Mb 的文件进行测试
并且加载栏无法正常工作
HTML:
<form id="cuadro" action="" class="dropzone">
</form>
<p id="texto_carga" style="color: #009688; display:none">Espera mientras se procesa el archivo...</p>
JS:
<script type="text/javascript">
var errors = false;
var Dropzone = new Dropzone("#cuadro", {
url: "../utilidades/pruebasupload.php?id=<?=$personaId?>&codigo=<?=$codigo?>",
acceptedFiles: ".EDF,.edf,.pdf,.PDF,.rar,.RAR,.jpg,.png,.gif",
maxFiles: 1,
error:function(){
errors = true;
},
processing:function(){
$('#texto_carga').show();
},
complete:function(){
if(errors){
swal({
title: 'Error al cargar el achivo!',
text: 'Ha ocurrido un error al intentar cargar el archivo. Póngase en contacto con el administrador del sistema',
type: 'error',
icon: 'error'
});
$('#texto_carga').hide();
}else{
swal({
title: 'Carga completa!',
text: 'Hemos cargado el archivo de la prueba exitosamente',
type: 'success',
icon: 'success'
});
$('#texto_carga').hide();
}
}
});
</script>
PHP pruebasupload.php
require_once "../terceros/dropbox/vendor/autoload.php";
require_once '../clases/servidor_archivos_controller.php';
require_once '../clases/conexion.php';
use Kunnu\Dropbox\Dropbox;
use Kunnu\Dropbox\DropboxApp;
use Kunnu\Dropbox\DropboxFile;
$conexion = new conexion;
$_servidor = new servidorArchivos;
$data = $_servidor->buscarConexion();
$dropboxKey = "";
$dropboxSecret = "";
$acessToken = "";
$appName= "";
$megas = "";
if(empty($data)){
$megas = 200000;
}else{
$dropboxKey = $data[0]['Keyapp'];
$dropboxSecret = $data[0]['Secret'];
$acessToken = $data[0]['Token'];
$appName= $data[0]['Appname'];
$megas = $data[0]['Megas'];
$megas = ($megas * 1024) * 1024 ;
}
if($tama[0]>$megas){
try{
//upload file to dropbox
$file = $dropbox->simpleUpload($tempFile,$nombredropbox, ['autorename' => true]);
//share a file
$response = $dropbox->postToAPI("/sharing/create_shared_link_with_settings", ["path" => $nombredropbox, "settings" => ['requested_visibility' => 'public']]);
$data = $response->getDecodedBody();
$link = $data['url'];
//save link to document in to DB
$query = "insert into pruebas_archivos (Codigo,Archivo,Ubicacion)values('$savecodge','$nombredropbox','2')";
$datos= $conexion->NonQuery($query);
http_response_code(200);
}catch(\EXCEPTION $e){
ERROR('001',$E);
http_response_code(400);
}
}else{
$targetPath = "../public/pruebas/";
$targetFile = $targetPath.$id ."_". $nombreCompleto; //5
move_uploaded_file($tempFile,$targetFile);
//save the url into DB
$query = "insert into pruebas_archivos (Codigo,Archivo,Ubicacion)values('$savecodge','$nombreCompleto','1')";
$conexion->NonQuery($query);
http_response_code(200);
}
}
function error($numero,$texto){
$ddf = fopen('error.log','a');
fwrite($ddf,"[".date("r")."] Error $numero: $texto\r\n");
fclose($ddf);
}
PHP 初始化文件
我已经测试了您的演示,有时能够上传大于 6mb 的文件。 有时,在上传时,脚本会在 30 秒时死掉。
因此,看起来你的代码逻辑没有问题,但是你的服务器出现了一些超时。正如您已经指出的那样,max_execution_time
和 max_input_time
似乎设置正确(我会用 phpinfo();
仔细检查),但取决于您是 运行 的服务器,您可能还有其他地方需要检查。
- 如果在 Apache 上是 运行,请检查
httpd.conf
和TimeOut
指令。 - 如果您使用的是
php-fpm
,请检查www.conf
和request_terminate_timeout
指令。 - 如果您使用的是 nginx,请检查
fastcgi_read_timeout
指令