修复错误的正则表达式
实现文件夹上传功能 *文件夹上传功能使用了webkitdirectory,故Firefox android版以及IOS设备不支持这一功能
This commit is contained in:
parent
468b37b85a
commit
a466b65f77
@ -15,7 +15,7 @@ use yii\web\UploadedFile;
|
|||||||
|
|
||||||
class HomeController extends Controller
|
class HomeController extends Controller
|
||||||
{
|
{
|
||||||
protected string $pattern = '/^[^\p{C}\/:*?"<>|\\\\]+$/u';
|
protected string $pattern = '/^[^\p{C}:*?"<>|\\\\]+$/u';
|
||||||
|
|
||||||
public function behaviors()
|
public function behaviors()
|
||||||
{
|
{
|
||||||
@ -261,6 +261,7 @@ class HomeController extends Controller
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
|
* 注意,已存在的同名文件会被覆盖
|
||||||
* https://devs.chenx221.cyou:8081/index.php?r=home%2Fupload
|
* https://devs.chenx221.cyou:8081/index.php?r=home%2Fupload
|
||||||
*
|
*
|
||||||
* @return string|Response
|
* @return string|Response
|
||||||
@ -275,7 +276,7 @@ class HomeController extends Controller
|
|||||||
|
|
||||||
foreach ($uploadedFiles as $uploadedFile) {
|
foreach ($uploadedFiles as $uploadedFile) {
|
||||||
$model->uploadFile = $uploadedFile;
|
$model->uploadFile = $uploadedFile;
|
||||||
if (!preg_match($this->pattern, $model->uploadFile->baseName)) {
|
if (!preg_match($this->pattern, $model->uploadFile->fullPath)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if ($model->upload()) {
|
if ($model->upload()) {
|
||||||
|
@ -35,9 +35,10 @@ class UploadForm extends Model
|
|||||||
if (!is_dir($absolutePath)) {
|
if (!is_dir($absolutePath)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$fileName = $this->uploadFile->baseName;
|
$fileName = $this->uploadFile->fullPath;
|
||||||
if ($this->uploadFile->extension !== '') {
|
$directory = dirname($absolutePath . '/' . $fileName);
|
||||||
$fileName .= '.' . $this->uploadFile->extension;
|
if (!is_dir($directory)) {
|
||||||
|
mkdir($directory, 0777, true);
|
||||||
}
|
}
|
||||||
$this->uploadFile->saveAs($absolutePath . '/' . $fileName);
|
$this->uploadFile->saveAs($absolutePath . '/' . $fileName);
|
||||||
return true;
|
return true;
|
||||||
|
@ -7,7 +7,6 @@
|
|||||||
/* @var $directory string 当前路径 */
|
/* @var $directory string 当前路径 */
|
||||||
|
|
||||||
use app\models\RenameForm;
|
use app\models\RenameForm;
|
||||||
use app\models\UploadForm;
|
|
||||||
use yii\bootstrap5\ActiveForm;
|
use yii\bootstrap5\ActiveForm;
|
||||||
use yii\bootstrap5\Html;
|
use yii\bootstrap5\Html;
|
||||||
use app\assets\FontAwesomeAsset;
|
use app\assets\FontAwesomeAsset;
|
||||||
@ -35,6 +34,7 @@ $this->registerCssFile('@web/css/home_style.css');
|
|||||||
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
|
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
|
||||||
<li hidden>
|
<li hidden>
|
||||||
<input type="file" id="file-input" name="uploadFile" multiple>
|
<input type="file" id="file-input" name="uploadFile" multiple>
|
||||||
|
<input type="file" id="folder-input" name="uploadFile" multiple webkitdirectory>
|
||||||
<input type="hidden" name="targetDir" value="<?= $directory ?>" id="target-dir">
|
<input type="hidden" name="targetDir" value="<?= $directory ?>" id="target-dir">
|
||||||
</li>
|
</li>
|
||||||
<li><?= Html::button('上传文件', ['class' => 'dropdown-item file-upload-btn']) ?></li>
|
<li><?= Html::button('上传文件', ['class' => 'dropdown-item file-upload-btn']) ?></li>
|
||||||
|
@ -49,7 +49,34 @@ $('#file-input').on('change', function () {
|
|||||||
|
|
||||||
|
|
||||||
$(document).on('click', '.folder-upload-btn', function () {
|
$(document).on('click', '.folder-upload-btn', function () {
|
||||||
console.log('你点击了上传文件夹,但功能尚未实现');
|
// 触发文件输入元素的点击事件
|
||||||
|
$('#folder-input').click();
|
||||||
|
});
|
||||||
|
|
||||||
|
$('#folder-input').on('change', function () {
|
||||||
|
$('#progress-bar').show();
|
||||||
|
var files = this.files;
|
||||||
|
var formData = new FormData();
|
||||||
|
for (var i = 0; i < files.length; i++) {
|
||||||
|
formData.append('files[]', files[i]);
|
||||||
|
}
|
||||||
|
formData.append('targetDir', $('#target-dir').val());
|
||||||
|
formData.append('_csrf', $('meta[name="csrf-token"]').attr('content'));
|
||||||
|
var xhr = new XMLHttpRequest();
|
||||||
|
xhr.upload.onprogress = function (event) {
|
||||||
|
if (event.lengthComputable) {
|
||||||
|
var percentComplete = event.loaded / event.total * 100;
|
||||||
|
$('#progress-bar .progress-bar').css('width', percentComplete + '%').text(Math.round(percentComplete) + '%');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
xhr.onload = function () {
|
||||||
|
if (xhr.status !== 200) {
|
||||||
|
alert('An error occurred during the upload.');
|
||||||
|
}
|
||||||
|
window.location.reload();
|
||||||
|
};
|
||||||
|
xhr.open('POST', 'index.php?r=home%2Fupload');
|
||||||
|
xhr.send(formData);
|
||||||
});
|
});
|
||||||
$(document).on('click', '.offline-download-btn', function () {
|
$(document).on('click', '.offline-download-btn', function () {
|
||||||
console.log('你点击了离线下载,但功能尚未实现');
|
console.log('你点击了离线下载,但功能尚未实现');
|
||||||
|
Loading…
Reference in New Issue
Block a user