更新 index.php
可以不依赖任何扩展的小而美的PHP图床程序。
This commit is contained in:
parent
cfa8f0a48b
commit
da1dfd7312
936
index.php
936
index.php
@ -1,492 +1,444 @@
|
|||||||
<?php
|
<?php
|
||||||
$base_upload_dir = __DIR__ . '/o/'; // 如存放在当前程序目录下直接/ 即可如果其他目录前后都要/包含,如:/s/
|
// 如存放在当前目录下直接/即可,如果选择其他目录前后都要/包含,如:/o/
|
||||||
|
$base_dir='/o/';
|
||||||
$max_filename_length = 180; // 最大文件名长度
|
|
||||||
|
// 处理文件上传
|
||||||
// 获取服务器的最大上传文件大小和内存限制
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
|
||||||
$max_upload_size = ini_get('upload_max_filesize');
|
$name = $_FILES['file']['name'];
|
||||||
$max_memory_limit = ini_get('memory_limit');
|
$file_tmp = $_FILES['file']['tmp_name'];
|
||||||
|
$error = $_FILES['file']['error'];
|
||||||
// 生成随机文件名
|
|
||||||
function generateRandomFileName($length = 10) {
|
// 检查上传错误,直接提示错误代码
|
||||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
if ($error !== UPLOAD_ERR_OK) {
|
||||||
$randomString = '';
|
echo '{"error":"上传失败,错误代码:'.$error.'"}';
|
||||||
for ($i = 0; $i < $length; $i++) {
|
exit;
|
||||||
$randomString .= $characters[rand(0, strlen($characters) - 1)];
|
}
|
||||||
}
|
|
||||||
return $randomString;
|
// 兼容跨平台路径
|
||||||
}
|
$md5 = md5_file($file_tmp);
|
||||||
|
$upload_dir = __DIR__ . $base_dir . substr($md5,0,1).'/'.substr($md5,1,1);
|
||||||
// 处理文件上传
|
// 检查并创建目录
|
||||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
|
if (!is_dir($upload_dir)) {
|
||||||
$name = $_FILES['file']['name'];
|
if (!mkdir($upload_dir, 0777, true) && !is_dir($upload_dir)) {
|
||||||
$file_tmp = $_FILES['file']['tmp_name'];
|
echo '{"error":"无法创建目录:'.$upload_dir.'"}';
|
||||||
$error = $_FILES['file']['error'];
|
exit;
|
||||||
|
}
|
||||||
// 获取当前年月,为防止报错,先设置时区
|
// 开始创建index.html
|
||||||
date_default_timezone_set('Asia/Shanghai');
|
file_put_contents($upload_dir .'/index.html', 'no access');
|
||||||
$year_month = date('Y/m');
|
file_put_contents( __DIR__ . $base_dir . substr($md5,0,1). '/index.html', 'no access');
|
||||||
$upload_dir = $base_upload_dir . $year_month;
|
}
|
||||||
|
|
||||||
// 检查并创建目录
|
// 根据文件内容生成MD5文件名,这样可以排除重复上传
|
||||||
if (!is_dir($upload_dir)) {
|
$FileName = md5_file($file_tmp). '.' . strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||||||
if (!mkdir($upload_dir, 0777, true) && !is_dir($upload_dir)) {
|
// 这里判断下重复文件不移动文件
|
||||||
echo json_encode(['error' => '无法创建目录: ' . $upload_dir]);
|
if (file_exists($target_file) || move_uploaded_file($file_tmp, $upload_dir . '/' .$FileName)) {
|
||||||
exit;
|
echo '{"upload_url":"http://'.$_SERVER['HTTP_HOST'] . $base_dir . substr($md5,0,1). '/' .substr($md5,1,1) .'/'. $FileName.'"}';
|
||||||
}
|
} else {
|
||||||
}
|
echo '{"error":"写入失败,无法保存文件"}';
|
||||||
|
}
|
||||||
// 生成文件扩展名
|
exit;
|
||||||
$file_extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
}
|
||||||
|
?>
|
||||||
// 生成随机文件名
|
<!DOCTYPE html>
|
||||||
$randomFileName = generateRandomFileName() . '.' . $file_extension;
|
<html lang="zh-CN">
|
||||||
|
<head>
|
||||||
// 使用相对路径获取目标文件
|
<meta charset="UTF-8">
|
||||||
$target_file = $upload_dir . '/' . $randomFileName;
|
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||||
|
<link rel="stylesheet" href="http://cdn.atusu.cn/202410/bootstrap.min.css">
|
||||||
// 检查上传错误
|
<script src="http://cdn.atusu.cn/202410/jquery.min.js"></script>
|
||||||
if ($error !== UPLOAD_ERR_OK) {
|
<title>一个简单的图床</title>
|
||||||
switch ($error) {
|
<style>
|
||||||
case UPLOAD_ERR_INI_SIZE:
|
html, body {
|
||||||
case UPLOAD_ERR_FORM_SIZE:
|
height: 100%;
|
||||||
echo json_encode(['error' => '文件过大,最大允许上传:' . $max_upload_size]);
|
margin: 0;
|
||||||
exit;
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
case UPLOAD_ERR_PARTIAL:
|
align-items: center;
|
||||||
echo json_encode(['error' => '文件部分上传失败']);
|
flex-direction: column;
|
||||||
exit;
|
}
|
||||||
|
.container {
|
||||||
case UPLOAD_ERR_NO_FILE:
|
width: 600px;
|
||||||
echo json_encode(['error' => '没有文件被上传']);
|
overflow: hidden;
|
||||||
exit;
|
}
|
||||||
|
.file-list {
|
||||||
case UPLOAD_ERR_CANT_WRITE:
|
max-height: 200px;
|
||||||
echo json_encode(['error' => '写入失败,无法保存文件']);
|
overflow-y: auto;
|
||||||
exit;
|
margin-bottom: 15px;
|
||||||
|
}
|
||||||
case UPLOAD_ERR_EXTENSION:
|
.file-row {
|
||||||
echo json_encode(['error' => '文件上传被扩展程序阻止']);
|
display: flex;
|
||||||
exit;
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
default:
|
padding: 10px 0;
|
||||||
echo json_encode(['error' => '上传失败,错误代码:' . $error]);
|
border-bottom: 1px solid #ccc;
|
||||||
exit;
|
margin-bottom: 5px;
|
||||||
}
|
}
|
||||||
}
|
.file-row:last-child {
|
||||||
|
border-bottom: none;
|
||||||
// 检查硬盘空间
|
}
|
||||||
if (disk_free_space($upload_dir) < filesize($file_tmp)) {
|
.file-name {
|
||||||
echo json_encode(['error' => '硬盘空间不足,请清理磁盘后再进行操作']);
|
flex-grow: 1;
|
||||||
exit;
|
width: 200px;
|
||||||
}
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
// 移动文件
|
white-space: nowrap;
|
||||||
if (move_uploaded_file($file_tmp, $target_file)) {
|
}
|
||||||
$domain = $_SERVER['HTTP_HOST'];
|
.file-size {
|
||||||
$relative_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $upload_dir);
|
margin-left: 10px;
|
||||||
$download_url = "http://$domain" . rtrim($relative_path, '/') . '/' . rawurlencode($randomFileName);
|
color: #666;
|
||||||
echo json_encode(['upload_url' => htmlspecialchars($download_url)]);
|
margin-right: 5px;
|
||||||
} else {
|
}
|
||||||
echo json_encode(['error' => '写入失败,无法保存文件']);
|
.progress {
|
||||||
}
|
width: 100px;
|
||||||
exit;
|
font-size: 12px;
|
||||||
}
|
margin-left: 5px;
|
||||||
?>
|
display: none;
|
||||||
<!DOCTYPE html>
|
border-radius: 5px;
|
||||||
<html lang="zh-CN">
|
}
|
||||||
<head>
|
.btn-download, .btn-copy {
|
||||||
<meta charset="UTF-8">
|
color: #ffffff;
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
width: 50px;
|
||||||
<link rel="stylesheet" href="http://cdn.atusu.cn/202410/bootstrap.min.css">
|
height: 25px;
|
||||||
<script src="http://cdn.atusu.cn/202410/jquery.min.js"></script>
|
padding: 0;
|
||||||
<title>一个简单的图床</title>
|
font-size: 14px;
|
||||||
<style>
|
margin-left: 5px;
|
||||||
html, body {
|
}
|
||||||
height: 100%;
|
.btn-download {
|
||||||
margin: 0;
|
background-color: #0d6efd;
|
||||||
display: flex;
|
}
|
||||||
justify-content: center;
|
.btn-copy {
|
||||||
align-items: center;
|
background-color: #198754;
|
||||||
flex-direction: column;
|
}
|
||||||
}
|
.status-output {
|
||||||
.container {
|
margin-top: 15px;
|
||||||
width: 600px;
|
height: 30px;
|
||||||
overflow: hidden;
|
padding: 5px;
|
||||||
}
|
overflow: hidden;
|
||||||
.file-list {
|
font-weight: 1000;
|
||||||
max-height: 200px;
|
font-size: 16px;
|
||||||
overflow-y: auto;
|
}
|
||||||
margin-bottom: 15px;
|
.link-output {
|
||||||
}
|
/*margin-top: 20px;
|
||||||
.file-row {
|
border: 1px solid #ccc;*/
|
||||||
display: flex;
|
padding: 10px;
|
||||||
justify-content: space-between;
|
display: none; /* 默认隐藏 */
|
||||||
align-items: center;
|
}
|
||||||
padding: 10px 0;
|
.link-output textarea {
|
||||||
border-bottom: 1px solid #ccc;
|
width: 100%;
|
||||||
margin-bottom: 5px;
|
height: 100px;
|
||||||
}
|
margin-top: 10px;
|
||||||
.file-row:last-child {
|
white-space: pre-wrap; /* 自动换行 */
|
||||||
border-bottom: none;
|
overflow-wrap: break-word; /* 强制换行 */
|
||||||
}
|
}
|
||||||
.file-name {
|
.copy-all-btn-container {
|
||||||
flex-grow: 1;
|
display: flex;
|
||||||
width: 200px;
|
justify-content: flex-end; /* 使按钮靠右对齐 */
|
||||||
overflow: hidden;
|
margin-top: 10px; /* 添加上边距 */
|
||||||
text-overflow: ellipsis;
|
}
|
||||||
white-space: nowrap;
|
[url=home.php?mod=space&uid=945662]@media[/url] (max-width: 600px) {
|
||||||
}
|
.container {
|
||||||
.file-size {
|
width: 100%;
|
||||||
margin-left: 10px;
|
padding: 0 10px;
|
||||||
color: #666;
|
}
|
||||||
margin-right: 5px;
|
.file-name {
|
||||||
}
|
width: 100px;
|
||||||
.progress {
|
overflow: hidden;
|
||||||
width: 100px;
|
text-overflow: ellipsis;
|
||||||
font-size: 12px;
|
white-space: nowrap;
|
||||||
margin-left: 5px;
|
}
|
||||||
display: none;
|
.progress-status {
|
||||||
border-radius: 5px;
|
display: none;
|
||||||
}
|
}
|
||||||
.btn-download, .btn-copy {
|
.btn-download, .btn-copy {
|
||||||
color: #ffffff;
|
width: 40px;
|
||||||
width: 50px;
|
margin-left: 5px;
|
||||||
height: 25px;
|
}
|
||||||
padding: 0;
|
}
|
||||||
font-size: 14px;
|
.nav {
|
||||||
margin-left: 5px;
|
display: flex;
|
||||||
}
|
justify-content: center; /* 居中对齐 */
|
||||||
.btn-download {
|
align-items: center; /* 垂直居中 */
|
||||||
background-color: #0d6efd;
|
gap: 20px; /* 链接之间的间距 */
|
||||||
}
|
}
|
||||||
.btn-copy {
|
</style>
|
||||||
background-color: #198754;
|
</head>
|
||||||
}
|
<body>
|
||||||
.status-output {
|
<div class="container">
|
||||||
margin-top: 15px;
|
<div class="card">
|
||||||
height: 30px;
|
<div class="card-body">
|
||||||
padding: 5px;
|
<h4 class="card-title text-center" style="font-weight: bold;">一个简单的图床</h4>
|
||||||
overflow: hidden;
|
<form id="uploadForm" enctype="multipart/form-data">
|
||||||
font-weight: 1000;
|
<div class="mb-3">
|
||||||
font-size: 16px;
|
<label for="files" class="form-label"></label>
|
||||||
}
|
<input type="file" class="form-control" id="files" name="files[]" multiple accept="image/jpeg, image/png, image/gif, image/webp, image/avif, image/bmp, image/svg+xml" required>
|
||||||
.link-output {
|
</div>
|
||||||
/*margin-top: 20px;
|
<button type="submit" class="btn btn-primary w-100" style="font-weight: bold;">开始上传</button>
|
||||||
border: 1px solid #ccc;*/
|
</form>
|
||||||
padding: 10px;
|
<div id="fileList" class="file-list mt-3"></div>
|
||||||
display: none; /* 默认隐藏 */
|
<div id="statusOutput" class="status-output"></div>
|
||||||
}
|
<div class="link-output">
|
||||||
.link-output textarea {
|
<button class="btn btn-info" id="directLinkBtn">直连代码</button>
|
||||||
width: 100%;
|
<button class="btn btn-info" id="htmlCodeBtn">网页代码</button>
|
||||||
height: 100px;
|
<button class="btn btn-info" id="forumCodeBtn">论坛代码</button>
|
||||||
margin-top: 10px;
|
<button class="btn btn-info" id="markDownCodeBtn">Markdown代码</button>
|
||||||
white-space: pre-wrap; /* 自动换行 */
|
<textarea id="linkText" readonly></textarea>
|
||||||
overflow-wrap: break-word; /* 强制换行 */
|
<div class="copy-all-btn-container">
|
||||||
}
|
<button class="btn btn-success" id="copyAllBtn">复制所有链接</button>
|
||||||
.copy-all-btn-container {
|
</div>
|
||||||
display: flex;
|
</div>
|
||||||
justify-content: flex-end; /* 使按钮靠右对齐 */
|
<h6 id="formatHint" class="card-title text-center">支持格式: JPEG, PNG, GIF, WEBP, AVIF, BMP, SVG 最大上传:<?php echo ini_get('upload_max_filesize');?></h6>
|
||||||
margin-top: 10px; /* 添加上边距 */
|
</div>
|
||||||
}
|
</div>
|
||||||
[url=home.php?mod=space&uid=945662]@media[/url] (max-width: 600px) {
|
</div>
|
||||||
.container {
|
</div>
|
||||||
width: 100%;
|
<script>
|
||||||
padding: 0 10px;
|
$(document).ready(function() {
|
||||||
}
|
// 初始化时显示格式提示
|
||||||
.file-name {
|
$('#formatHint, #serverWarning, #navLinks').show();
|
||||||
width: 100px;
|
|
||||||
overflow: hidden;
|
$('#files').on('change', function() {
|
||||||
text-overflow: ellipsis;
|
if (this.files.length > 0) {
|
||||||
white-space: nowrap;
|
$('#formatHint, #serverWarning, #navLinks').hide(); // 隐藏提示信息
|
||||||
}
|
} else {
|
||||||
.progress-status {
|
$('#formatHint, #serverWarning, #navLinks').show(); // 显示提示信息
|
||||||
display: none;
|
}
|
||||||
}
|
});
|
||||||
.btn-download, .btn-copy {
|
});
|
||||||
width: 40px;
|
let uploadedLinks = [];
|
||||||
margin-left: 5px;
|
$(document).ready(function () {
|
||||||
}
|
// 清除输出信息的函数
|
||||||
}
|
function clearOutput() {
|
||||||
.nav {
|
$('#fileList').empty();
|
||||||
display: flex;
|
$('#statusOutput').text('当前状态: 等待上传...');
|
||||||
justify-content: center; /* 居中对齐 */
|
$('.link-output').hide();
|
||||||
align-items: center; /* 垂直居中 */
|
$('#linkText').val('');
|
||||||
gap: 20px; /* 链接之间的间距 */
|
uploadedLinks = [];
|
||||||
}
|
}
|
||||||
</style>
|
// 点击选择文件框时清除输出信息
|
||||||
</head>
|
$('#files').on('click', function () {
|
||||||
<body>
|
clearOutput();
|
||||||
<div class="container">
|
});
|
||||||
<div class="card">
|
$('#files').on('change', function () {
|
||||||
<div class="card-body">
|
clearOutput();
|
||||||
<h4 class="card-title text-center" style="font-weight: bold;">一个简单的图床</h4>
|
var files = this.files;
|
||||||
<form id="uploadForm" enctype="multipart/form-data">
|
// 允许的文件类型
|
||||||
<div class="mb-3">
|
var validFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/bmp', 'image/svg'];
|
||||||
<label for="files" class="form-label"></label>
|
var validFiles = Array.from(files).filter(file => validFormats.includes(file.type));
|
||||||
<input type="file" class="form-control" id="files" name="files[]" multiple accept="image/jpeg, image/png, image/gif, image/webp, image/avif, image/bmp, image/svg+xml" required>
|
|
||||||
</div>
|
if (validFiles.length === 0) {
|
||||||
<button type="submit" class="btn btn-primary w-100" style="font-weight: bold;">开始上传</button>
|
showAlert('请选择有效的图片文件(JPEG, PNG, GIF,WEBP,AVIF,BMP,SVG)!');
|
||||||
</form>
|
$('#files').val(''); // 清空文件选择
|
||||||
<div id="fileList" class="file-list mt-3"></div>
|
return;
|
||||||
<div id="statusOutput" class="status-output"></div>
|
}
|
||||||
<div class="link-output">
|
$.each(validFiles, function (index, file) {
|
||||||
<button class="btn btn-info" id="directLinkBtn">直接连接</button>
|
var fileSize = (file.size / (1024 * 1024)).toFixed(2) + ' MB';
|
||||||
<button class="btn btn-info" id="htmlCodeBtn">网页代码</button>
|
var fileRow = `
|
||||||
<button class="btn btn-info" id="forumCodeBtn">论坛代码</button>
|
<div class="file-row" id="file-row-${index}">
|
||||||
<textarea id="linkText" readonly></textarea>
|
<span class="file-name">${file.name}</span>
|
||||||
<div class="copy-all-btn-container">
|
<span class="file-size">(${fileSize})</span>
|
||||||
<button class="btn btn-success" id="copyAllBtn">复制所有链接</button>
|
<span class="progress-status">未上传</span>
|
||||||
</div>
|
<div class="progress">
|
||||||
</div>
|
<div class="progress-bar progress-bar-striped" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
|
||||||
<h6 id="formatHint" class="card-title text-center">支持格式: JPEG, PNG, GIF, WEBP, AVIF, BMP, SVG</h6>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
`;
|
||||||
</div>
|
$('#fileList').append(fileRow);
|
||||||
</div>
|
});
|
||||||
<script>
|
});
|
||||||
$(document).ready(function() {
|
$('#uploadForm').on('submit', function (event) {
|
||||||
// 初始化时显示格式提示
|
event.preventDefault();
|
||||||
$('#formatHint, #serverWarning, #navLinks').show();
|
var files = $('#files')[0].files;
|
||||||
|
var fileIndex = 0;
|
||||||
$('#files').on('change', function() {
|
function uploadNextFile() {
|
||||||
if (this.files.length > 0) {
|
if (fileIndex < files.length) {
|
||||||
$('#formatHint, #serverWarning, #navLinks').hide(); // 隐藏提示信息
|
var formData = new FormData();
|
||||||
} else {
|
formData.append('file', files[fileIndex]);
|
||||||
$('#formatHint, #serverWarning, #navLinks').show(); // 显示提示信息
|
var xhr = new XMLHttpRequest();
|
||||||
}
|
xhr.open("POST", '', true);
|
||||||
});
|
var startTime;
|
||||||
});
|
xhr.upload.addEventListener('progress', function (evt) {
|
||||||
let uploadedLinks = [];
|
if (evt.lengthComputable) {
|
||||||
$(document).ready(function () {
|
if (!startTime) startTime = new Date();
|
||||||
// 清除输出信息的函数
|
var percentComplete = Math.round((evt.loaded / evt.total) * 100);
|
||||||
function clearOutput() {
|
var elapsedTime = (new Date() - startTime) / 1000;
|
||||||
$('#fileList').empty();
|
var speed = (evt.loaded / 1024 / 1024) / elapsedTime;
|
||||||
$('#statusOutput').text('当前状态: 等待上传...');
|
var fileRow = $('#file-row-' + fileIndex);
|
||||||
$('.link-output').hide();
|
fileRow.find('.progress-status').text(` ${speed.toFixed(2)} MB/s`);
|
||||||
$('#linkText').val('');
|
fileRow.find('.progress').show();
|
||||||
uploadedLinks = [];
|
fileRow.find('.progress-bar').css('width', percentComplete + '%').text(percentComplete + '%');
|
||||||
}
|
var container = $('#fileList');
|
||||||
// 点击选择文件框时清除输出信息
|
var targetRow = $('#file-row-' + fileIndex);
|
||||||
$('#files').on('click', function () {
|
var targetTop = targetRow[0].offsetTop;
|
||||||
clearOutput();
|
var containerHeight = container.height();
|
||||||
});
|
var rowHeight = targetRow.outerHeight();
|
||||||
$('#files').on('change', function () {
|
var scrollTo = targetTop - (containerHeight / 1) + (rowHeight / 2);
|
||||||
clearOutput();
|
scrollTo = Math.max(0, Math.min(scrollTo, container[0].scrollHeight - containerHeight));
|
||||||
var files = this.files;
|
container.stop().animate({ scrollTop: scrollTo }, 200);
|
||||||
// 允许的文件类型
|
if (percentComplete === 100) {
|
||||||
var validFormats = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'image/avif', 'image/bmp', 'image/svg'];
|
setTimeout(function () {
|
||||||
var validFiles = Array.from(files).filter(file => validFormats.includes(file.type));
|
if (xhr.readyState !== XMLHttpRequest.DONE) {
|
||||||
|
fileRow.find('.progress-status').text('处理中请稍等');
|
||||||
if (validFiles.length === 0) {
|
}
|
||||||
showAlert('请选择有效的图片文件(JPEG, PNG, GIF,WEBP,BMP,SVG)!');
|
}, 100);
|
||||||
$('#files').val(''); // 清空文件选择
|
}
|
||||||
return;
|
}
|
||||||
}
|
}, false);
|
||||||
$.each(validFiles, function (index, file) {
|
xhr.onreadystatechange = function () {
|
||||||
var fileSize = (file.size / (1024 * 1024)).toFixed(2) + ' MB';
|
if (xhr.readyState === XMLHttpRequest.DONE) {
|
||||||
var fileRow = `
|
var fileRow = $('#file-row-' + fileIndex);
|
||||||
<div class="file-row" id="file-row-${index}">
|
if (xhr.status === 200) {
|
||||||
<span class="file-name">${file.name}</span>
|
var result = JSON.parse(xhr.responseText);
|
||||||
<span class="file-size">(${fileSize})</span>
|
if (result.upload_url) {
|
||||||
<span class="progress-status">未上传</span>
|
uploadedLinks.push(result.upload_url);
|
||||||
<div class="progress">
|
fileRow.find('.progress-status').text('处理完成');
|
||||||
<div class="progress-bar progress-bar-striped" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
|
fileRow.find('.progress').hide();
|
||||||
</div>
|
var linkHtml = `
|
||||||
</div>
|
<a href="${result.upload_url}" target="_blank" class="btn btn-info btn-download">预览</a>
|
||||||
`;
|
<button class="btn btn-success btn-copy copyButton" data-url="${result.upload_url}">复制</button>
|
||||||
$('#fileList').append(fileRow);
|
`;
|
||||||
});
|
fileRow.append(linkHtml);
|
||||||
});
|
setTimeout(function() {
|
||||||
$('#uploadForm').on('submit', function (event) {
|
fileRow.find('.progress-status').text('成功上传');
|
||||||
event.preventDefault();
|
}, 100);
|
||||||
var files = $('#files')[0].files;
|
showLinkOutput();
|
||||||
var fileIndex = 0;
|
} else if (result.error) {
|
||||||
function uploadNextFile() {
|
fileRow.find('.progress-status').text('上传失败: ' + result.error);
|
||||||
if (fileIndex < files.length) {
|
$('#statusOutput').text('上传中断: ' + result.error);
|
||||||
var formData = new FormData();
|
return;
|
||||||
formData.append('file', files[fileIndex]);
|
}
|
||||||
var xhr = new XMLHttpRequest();
|
} else {
|
||||||
xhr.open("POST", '', true);
|
fileRow.find('.progress-status').text('错误信息: ' + xhr.statusText);
|
||||||
var startTime;
|
$('#statusOutput').text('上传中断: ' + xhr.statusText);
|
||||||
xhr.upload.addEventListener('progress', function (evt) {
|
return;
|
||||||
if (evt.lengthComputable) {
|
}
|
||||||
if (!startTime) startTime = new Date();
|
fileIndex++;
|
||||||
var percentComplete = Math.round((evt.loaded / evt.total) * 100);
|
uploadNextFile();
|
||||||
var elapsedTime = (new Date() - startTime) / 1000;
|
}
|
||||||
var speed = (evt.loaded / 1024 / 1024) / elapsedTime;
|
};
|
||||||
var fileRow = $('#file-row-' + fileIndex);
|
xhr.send(formData);
|
||||||
fileRow.find('.progress-status').text(` ${speed.toFixed(2)} MB/s`);
|
} else {
|
||||||
fileRow.find('.progress').show();
|
$('#statusOutput').text('上传任务操作完成');
|
||||||
fileRow.find('.progress-bar').css('width', percentComplete + '%').text(percentComplete + '%');
|
$('#files').val(''); // 清空文件选择
|
||||||
var container = $('#fileList');
|
}
|
||||||
var targetRow = $('#file-row-' + fileIndex);
|
$('#statusOutput').text(`当前状态: 正在上传 ${files[fileIndex].name}`);
|
||||||
var targetTop = targetRow[0].offsetTop;
|
}
|
||||||
var containerHeight = container.height();
|
uploadNextFile();
|
||||||
var rowHeight = targetRow.outerHeight();
|
});
|
||||||
var scrollTo = targetTop - (containerHeight / 1) + (rowHeight / 2);
|
function showLinkOutput() {
|
||||||
scrollTo = Math.max(0, Math.min(scrollTo, container[0].scrollHeight - containerHeight));
|
if (uploadedLinks.length > 1) {
|
||||||
container.stop().animate({ scrollTop: scrollTo }, 200);
|
$('.link-output').show();
|
||||||
if (percentComplete === 100) {
|
$('#linkText').val(uploadedLinks.join('\n')); // 显示所有链接
|
||||||
setTimeout(function () {
|
$('#linkText').scrollTop($('#linkText')[0].scrollHeight);
|
||||||
if (xhr.readyState !== XMLHttpRequest.DONE) {
|
}
|
||||||
fileRow.find('.progress-status').text('处理中请稍等');
|
}
|
||||||
}
|
function showAlert(message) {
|
||||||
}, 100);
|
var tooltip = document.createElement('div');
|
||||||
}
|
tooltip.style.position = 'fixed';
|
||||||
}
|
tooltip.style.background = '#ffffff';
|
||||||
}, false);
|
tooltip.style.border = '2px solid #0d6efd';
|
||||||
xhr.onreadystatechange = function () {
|
tooltip.style.padding = '10px';
|
||||||
if (xhr.readyState === XMLHttpRequest.DONE) {
|
tooltip.style.zIndex = 1000;
|
||||||
var fileRow = $('#file-row-' + fileIndex);
|
tooltip.style.left = '50%';
|
||||||
if (xhr.status === 200) {
|
tooltip.style.top = '50%';
|
||||||
var result = JSON.parse(xhr.responseText);
|
tooltip.style.transform = 'translate(-50%, -50%)';
|
||||||
if (result.upload_url) {
|
tooltip.innerHTML = '<strong>' + message + '</strong>';
|
||||||
uploadedLinks.push(result.upload_url);
|
document.body.appendChild(tooltip);
|
||||||
fileRow.find('.progress-status').text('处理完成');
|
setTimeout(function () {
|
||||||
fileRow.find('.progress').hide();
|
document.body.removeChild(tooltip);
|
||||||
var linkHtml = `
|
}, 3000);
|
||||||
<a href="${result.upload_url}" target="_blank" class="btn btn-info btn-download">预览</a>
|
}
|
||||||
<button class="btn btn-success btn-copy copyButton" data-url="${result.upload_url}">复制</button>
|
|
||||||
`;
|
$('#directLinkBtn').on('click', function () {
|
||||||
fileRow.append(linkHtml);
|
$('#linkText').val(uploadedLinks.join('\n'));
|
||||||
setTimeout(function() {
|
});
|
||||||
fileRow.find('.progress-status').text('成功上传');
|
|
||||||
}, 100);
|
$('#htmlCodeBtn').on('click', function () {
|
||||||
showLinkOutput();
|
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
||||||
} else if (result.error) {
|
$('#linkText').val(htmlCodes);
|
||||||
fileRow.find('.progress-status').text('上传失败: ' + result.error);
|
});
|
||||||
$('#statusOutput').text('上传中断: ' + result.error);
|
|
||||||
return;
|
$('#forumCodeBtn').on('click', function () {
|
||||||
}
|
const forumCodes = uploadedLinks.map(url => `[img]${url}[/img]`).join('\n');
|
||||||
} else {
|
$('#linkText').val(forumCodes);
|
||||||
fileRow.find('.progress-status').text('错误信息: ' + xhr.statusText);
|
});
|
||||||
$('#statusOutput').text('上传中断: ' + xhr.statusText);
|
|
||||||
return;
|
$('#markDownCodeBtn').on('click', function () {
|
||||||
}
|
const forumCodes = uploadedLinks.map(url => `![${url.replace(/^.*[\\\/]/, '')}](${url})`).join('\n');
|
||||||
fileIndex++;
|
$('#linkText').val(forumCodes);
|
||||||
uploadNextFile();
|
});
|
||||||
}
|
|
||||||
};
|
$('#htmlCodeBtn').on('click', function () {
|
||||||
xhr.send(formData);
|
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
||||||
} else {
|
$('#linkText').val(htmlCodes);
|
||||||
$('#statusOutput').text('上传任务操作完成');
|
});
|
||||||
$('#files').val(''); // 清空文件选择
|
|
||||||
}
|
$('#htmlCodeBtn').on('click', function () {
|
||||||
$('#statusOutput').text(`当前状态: 正在上传 ${files[fileIndex].name}`);
|
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
||||||
}
|
$('#linkText').val(htmlCodes);
|
||||||
uploadNextFile();
|
});
|
||||||
});
|
|
||||||
function showLinkOutput() {
|
$('#copyAllBtn').on('click', function () {
|
||||||
if (uploadedLinks.length > 1) {
|
var copyText = $('#linkText').val();
|
||||||
$('.link-output').show();
|
var tempInput = document.createElement('textarea');
|
||||||
$('#linkText').val(uploadedLinks.join('\n')); // 显示所有链接
|
tempInput.value = copyText;
|
||||||
$('#linkText').scrollTop($('#linkText')[0].scrollHeight);
|
document.body.appendChild(tempInput);
|
||||||
}
|
tempInput.select();
|
||||||
}
|
document.execCommand("copy");
|
||||||
function showAlert(message) {
|
document.body.removeChild(tempInput);
|
||||||
var tooltip = document.createElement('div');
|
|
||||||
tooltip.style.position = 'fixed';
|
var links = copyText.split('\n');
|
||||||
tooltip.style.background = '#ffffff';
|
var totalLinks = links.length;
|
||||||
tooltip.style.border = '2px solid #0d6efd';
|
var displayedLinks = links.slice(0, 10);
|
||||||
tooltip.style.padding = '10px';
|
var ignoredCount = totalLinks > 10 ? totalLinks - 10 : 0;
|
||||||
tooltip.style.zIndex = 1000;
|
|
||||||
tooltip.style.left = '50%';
|
var tooltip = document.createElement('div');
|
||||||
tooltip.style.top = '50%';
|
tooltip.style.position = 'fixed';
|
||||||
tooltip.style.transform = 'translate(-50%, -50%)';
|
tooltip.style.background = '#ffffff';
|
||||||
tooltip.innerHTML = '<strong>' + message + '</strong>';
|
tooltip.style.border = '2px solid #0d6efd';
|
||||||
document.body.appendChild(tooltip);
|
tooltip.style.padding = '10px';
|
||||||
setTimeout(function () {
|
tooltip.style.zIndex = 1000;
|
||||||
document.body.removeChild(tooltip);
|
tooltip.style.left = '50%';
|
||||||
}, 3000);
|
tooltip.style.top = '50%';
|
||||||
}
|
tooltip.style.transform = 'translate(-50%, -50%)';
|
||||||
|
|
||||||
$('#directLinkBtn').on('click', function () {
|
tooltip.innerHTML = `<strong>已成功复制所有代码到粘贴板,一共 ${totalLinks} 个链接:</strong><br><code>${displayedLinks.map(link => link.replace(/</g, '<').replace(/>/g, '>')).join('<br>')}</code>`;
|
||||||
$('#linkText').val(uploadedLinks.join('\n'));
|
|
||||||
});
|
if (ignoredCount > 0) {
|
||||||
|
tooltip.innerHTML += `<br><em>提示框仅显示10个,其余${ignoredCount} 个链接也已全部复制到粘贴板</em>`;
|
||||||
$('#htmlCodeBtn').on('click', function () {
|
}
|
||||||
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
|
||||||
$('#linkText').val(htmlCodes);
|
document.body.appendChild(tooltip);
|
||||||
});
|
|
||||||
|
setTimeout(function () {
|
||||||
$('#forumCodeBtn').on('click', function () {
|
document.body.removeChild(tooltip);
|
||||||
const forumCodes = uploadedLinks.map(url => `[img]${url}[/img]`).join('\n');
|
}, 2200);
|
||||||
$('#linkText').val(forumCodes);
|
});
|
||||||
});
|
|
||||||
|
|
||||||
$('#htmlCodeBtn').on('click', function () {
|
$(document).on('click', '.copyButton', function () {
|
||||||
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
var copyText = $(this).data('url');
|
||||||
$('#linkText').val(htmlCodes);
|
var tempInput = document.createElement('input');
|
||||||
});
|
tempInput.value = copyText;
|
||||||
|
document.body.appendChild(tempInput);
|
||||||
$('#htmlCodeBtn').on('click', function () {
|
tempInput.select();
|
||||||
const htmlCodes = uploadedLinks.map(url => `<img src="${url}">`).join('\n');
|
document.execCommand("copy");
|
||||||
$('#linkText').val(htmlCodes);
|
document.body.removeChild(tempInput);
|
||||||
});
|
var tooltip = document.createElement('div');
|
||||||
|
tooltip.style.position = 'fixed';
|
||||||
$('#copyAllBtn').on('click', function () {
|
tooltip.style.background = '#ffffff';
|
||||||
var copyText = $('#linkText').val();
|
tooltip.style.border = '2px solid #0d6efd';
|
||||||
var tempInput = document.createElement('textarea');
|
tooltip.style.padding = '10px';
|
||||||
tempInput.value = copyText;
|
tooltip.style.zIndex = 1000;
|
||||||
document.body.appendChild(tempInput);
|
tooltip.style.left = '50%';
|
||||||
tempInput.select();
|
tooltip.style.top = '50%';
|
||||||
document.execCommand("copy");
|
tooltip.style.transform = 'translate(-50%, -50%)';
|
||||||
document.body.removeChild(tempInput);
|
tooltip.innerHTML = '<strong>链接已成功复制到粘贴板:</strong><br>' + copyText;
|
||||||
|
document.body.appendChild(tooltip)
|
||||||
var links = copyText.split('\n');
|
setTimeout(function () {
|
||||||
var totalLinks = links.length;
|
document.body.removeChild(tooltip);
|
||||||
var displayedLinks = links.slice(0, 10);
|
}, 2000);
|
||||||
var ignoredCount = totalLinks > 10 ? totalLinks - 10 : 0;
|
});
|
||||||
|
});
|
||||||
var tooltip = document.createElement('div');
|
</script>
|
||||||
tooltip.style.position = 'fixed';
|
</body>
|
||||||
tooltip.style.background = '#ffffff';
|
</html>
|
||||||
tooltip.style.border = '2px solid #0d6efd';
|
|
||||||
tooltip.style.padding = '10px';
|
|
||||||
tooltip.style.zIndex = 1000;
|
|
||||||
tooltip.style.left = '50%';
|
|
||||||
tooltip.style.top = '50%';
|
|
||||||
tooltip.style.transform = 'translate(-50%, -50%)';
|
|
||||||
|
|
||||||
tooltip.innerHTML = `<strong>已成功复制所有代码到粘贴板,一共 ${totalLinks} 个链接:</strong><br><code>${displayedLinks.map(link => link.replace(/</g, '<').replace(/>/g, '>')).join('<br>')}</code>`;
|
|
||||||
|
|
||||||
if (ignoredCount > 0) {
|
|
||||||
tooltip.innerHTML += `<br><em>提示框仅显示10个,其余${ignoredCount} 个链接也已全部复制到粘贴板</em>`;
|
|
||||||
}
|
|
||||||
|
|
||||||
document.body.appendChild(tooltip);
|
|
||||||
|
|
||||||
setTimeout(function () {
|
|
||||||
document.body.removeChild(tooltip);
|
|
||||||
}, 2200);
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
$(document).on('click', '.copyButton', function () {
|
|
||||||
var copyText = $(this).data('url');
|
|
||||||
var tempInput = document.createElement('input');
|
|
||||||
tempInput.value = copyText;
|
|
||||||
document.body.appendChild(tempInput);
|
|
||||||
tempInput.select();
|
|
||||||
document.execCommand("copy");
|
|
||||||
document.body.removeChild(tempInput);
|
|
||||||
var tooltip = document.createElement('div');
|
|
||||||
tooltip.style.position = 'fixed';
|
|
||||||
tooltip.style.background = '#ffffff';
|
|
||||||
tooltip.style.border = '2px solid #0d6efd';
|
|
||||||
tooltip.style.padding = '10px';
|
|
||||||
tooltip.style.zIndex = 1000;
|
|
||||||
tooltip.style.left = '50%';
|
|
||||||
tooltip.style.top = '50%';
|
|
||||||
tooltip.style.transform = 'translate(-50%, -50%)';
|
|
||||||
tooltip.innerHTML = '<strong>链接已成功复制到粘贴板:</strong><br>' + copyText;
|
|
||||||
document.body.appendChild(tooltip)
|
|
||||||
setTimeout(function () {
|
|
||||||
document.body.removeChild(tooltip);
|
|
||||||
}, 2000);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user