From 79128529bfc3f2c7ce996981d8782e152c2e9a84 Mon Sep 17 00:00:00 2001 From: jiangzhixiong <710328466@qq.com> Date: Tue, 9 Apr 2024 09:36:15 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=B3=A8=E5=85=A5webview=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.css | 6 ++++-- src/index.html | 1 + src/index.ts | 3 ++- src/utils/upload.ts | 27 ++++++++++++++------------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/index.css b/src/index.css index 8856f90..9493407 100644 --- a/src/index.css +++ b/src/index.css @@ -2,6 +2,8 @@ body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif; margin: auto; - max-width: 38rem; - padding: 2rem; } + +webview { + height: 100vh; +} \ No newline at end of file diff --git a/src/index.html b/src/index.html index efb92ed..e5f42a3 100644 --- a/src/index.html +++ b/src/index.html @@ -7,5 +7,6 @@
+ diff --git a/src/index.ts b/src/index.ts index 22abd62..00d0b69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,7 +15,8 @@ const createWindow = (): void => { height: 600, width: 800, webPreferences: { - preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY + preload: MAIN_WINDOW_PRELOAD_WEBPACK_ENTRY, + webviewTag: true }, }); diff --git a/src/utils/upload.ts b/src/utils/upload.ts index f80233e..c441bf2 100644 --- a/src/utils/upload.ts +++ b/src/utils/upload.ts @@ -5,7 +5,7 @@ import path from 'node:path'; // eslint-disable-next-line @typescript-eslint/no-unused-vars export const uploadFile = (filePaths: string[], _sender: Electron.WebContents) => { // 获取用户当前文件夹路径 - const saveDirectoryPath = app.getPath('userData'); + const saveDirectoryPath = app.getPath('downloads'); for (let i = 0; i < filePaths.length; i++) { const fileName = path.basename(filePaths[i]); const targetFilePath = path.join(saveDirectoryPath, fileName); @@ -35,17 +35,17 @@ export const uploadFile = (filePaths: string[], _sender: Electron.WebContents) = } export const singleUpload = (file: File) => { - let path = file.path; //文件本地路径 - let stats = fs.statSync(path); //读取文件信息 - let chunkSize = 3 * 1024 * 1024; //每片分块的大小3M - let size = stats.size; //文件大小 - let pieces = Math.ceil(size / chunkSize); //总共的分片数 + const path = file.path; //文件本地路径 + const stats = fs.statSync(path); //读取文件信息 + const chunkSize = 3 * 1024 * 1024; //每片分块的大小3M + const size = stats.size; //文件大小 + const pieces = Math.ceil(size / chunkSize); //总共的分片数 function uploadPiece (i: number) { //计算每块的结束位置 - let enddata = Math.min(size, (i + 1) * chunkSize); - let arr: any[] = []; - //创建一个readStream对象,根据文件起始位置和结束位置读取固定的分片 - let readStream = fs.createReadStream(path, { start: i * chunkSize, end: enddata - 1 }); + const endData = Math.min(size, (i + 1) * chunkSize); + const arr: any[] = []; + //创建一个readStream对象,根据文件起始位置和结束位置读取固定的分片 + const readStream = fs.createReadStream(path, { start: i * chunkSize, end: endData - 1 }); //on data读取数据 readStream.on('data', (data)=>{ arr.push(data) @@ -53,13 +53,14 @@ export const singleUpload = (file: File) => { //on end在该分片读取完成时触发 readStream.on('end', ()=>{ //这里服务端只接受blob对象,需要把原始的数据流转成blob对象,这块为了配合后端才转 - let blob = new Blob(arr) + const blob = new Blob(arr) //新建formdata数据对象 - var formdata = new FormData(); + const formdata = new FormData(); formdata.append("file", blob); console.log('blob.size',blob.size) formdata.append("size", size + ''); // 数字30被转换成字符串"30" formdata.append("chunk", i + '');//第几个分片,从0开始 formdata.append("chunks", pieces + '');//分片数 - } + }) + } } \ No newline at end of file