// 导出一些常量,包括上传信息、允许上传的文件类型、块大小和上传接口的URL // 这些常量将在前端和后端代码中使用 const BASE_URL = 'http://127.0.0.1:8080'; /** * dic - key : value * @type {{UPLOAD_FAILED: string, NO_FILE: string, INVALID_TYPE: string, UPLOAD_SUCCESS: string}} */ export const UPLOAD_INFO = { 'NO_FILE': '请先选择文件', 'INVALID_TYPE': '不支持该类型文件上传', 'UPLOAD_FAILED': '上传失败', 'UPLOAD_SUCCESS': '上传成功' }; export const ALLOWED_TYPE = { // 'video/mp4': 'mp4', // 'video/ogg': 'ogg' 'text/csv': 'csv', 'text/plain': 'txt' }; export const CHUNK_SIZE = 64 * 1024; export const API_URLS = { UPLOAD_FILE: BASE_URL + '/api/pregen/upload', START_GEN: BASE_URL + '/api/pregen/generate' }; // 在闭包内部,定义一个立即执行的匿名函数,接受document作为参数 ((doc) => { // 获取页面上的一些元素 const uploadProgressBar = doc.querySelector('#uploadProgressBar'); // 上传进度条 const fileUploadInput = doc.querySelector('#fileUploadInput'); // 文件上传输入框 const uploadInfo = doc.querySelector('#uploadInfo'); // p,显示上传状态信息 const uploadButton = doc.querySelector('#uploadButton'); // 上传按钮 const requirementArea = $('#requirementArea'); const genButton = doc.querySelector('#genButton'); // 开始生成按钮 const genInfo = $('#genInfo'); // p,显示生成状态信息 const downloadButton = doc.querySelector('#downloadButton'); const downloadInfo = $('#downloadInfo'); let uploadedSize = 0; // 已上传的文件大小,用于控制分块上传 /** * 初始化函数 */ const init = () => { // 绑定事件函数 - 为元素添加事件监听器 uploadButton.addEventListener('click', uploadFile, false); // uploadButton -> uploadFile() genButton.addEventListener('click', startGen, false); // genButton -> startGen() downloadButton.addEventListener('click', onDownload, false); // downloadButton -> onDownload() } /** * 创建FormData函数,将文件块和相关信息添加到FormData对象中 */ function createFormData({ name, type, size, fileName, uploadedSize, file }) { const fd = new FormData(); fd.append('name', name); fd.append('type', type); fd.append('size', size); fd.append('fileName', fileName); fd.append('uploadedSize', uploadedSize); fd.append('file', file); return fd; } /** * 创建视频元素函数,用于在页面上展示上传成功的视频 */ // function createFile(src) { // const uploadedVideo = document.createElement('file'); // uploadedVideo.controls = true; // uploadedVideo.width = '500'; // uploadedVideo.src = src; // document.body.appendChild(uploadedVideo); // } /** * 异步上传函数 */ async function uploadFile() { console.log("uploadFile()"); // 获取文件对象 const {files: [file]} = fileUploadInput; // 检查是否选择了文件,如果没有则显示提示信息 if (!file) { console.log("No file chosen"); uploadInfo.innerText = UPLOAD_INFO['NO_FILE']; return; } // 检查文件类型是否被允许,如果不允许则显示提示信息 console.log(file.type.toString()); if (!ALLOWED_TYPE[file.type]) { console.log("Invalid file chosen"); uploadInfo.innerText = UPLOAD_INFO['INVALID_TYPE']; return; } // 获取文件的一些基本信息 const {name, type, size} = file; const fileName = new Date().getTime() + '_' + name; let uploadedResult = null; uploadProgressBar.max = size; uploadInfo.innerText = ''; while (uploadedSize < size) { // 若文件大,使用slice方法将文件切割成小块,实现分块上传 const fileChunk = file.slice(uploadedSize, uploadedSize + CHUNK_SIZE); // 创建FormData对象,用于传递文件块和相关信息 const formData = createFormData({ name, type, size, fileName, uploadedSize, file: fileChunk }); console.log("axios POST"); // 使用axios库发送POST请求,将文件块上传到后端 try { uploadedResult = await axios.post(API_URLS.UPLOAD_FILE, formData); } catch (e) { uploadInfo.innerText = `${UPLOAD_INFO['UPLOAD_FAILED']}(${e.message})`; // on Exception return; } // 更新已上传的文件大小和进度条 uploadedSize += fileChunk.size; uploadProgressBar.value = uploadedSize; } // 上传成功后,显示上传成功的提示信息,并将文件输入框置空 uploadInfo.innerText = UPLOAD_INFO['UPLOAD_SUCCESS']; fileUploadInput.value = null; // 根据后端返回的视频URL,创建一个视频元素并展示在页面上 // createFile(uploadedResult.data.video_url); } async function startGen() { console.log("startGen()"); let requirement = requirementArea.val(); // Test console.log(requirement); $.ajax({ url: API_URLS["START_GEN"], type: "POST", data: requirement, dataType: "text", contentType: "text/plain", beforeSend() { genInfo.text("等待中..."); }, success(res) { // responding with String genInfo.text("生成完成!"); console.log("startGen() success"); } }); } async function onDownload() { // 替换为你的Spring Boot Controller 的下载接口地址 let downloadUrl = "/download?path=/path/to/your/file"; // 创建一个隐藏的元素来触发下载 let link = document.createElement('a'); link.href = downloadUrl; link.target = "_blank"; // 在新窗口中打开 link.download = "BotGen.txt"; // 下载后的文件名,可以自定义 link.style.display = 'none'; document.body.appendChild(link); link.click(); document.body.removeChild(link); } // ------------------------------------ /** * 初始化,执行绑定事件函数 */ init(); }) (document);