1. 程式人生 > >使用exceljs時報錯:no such file or directory

使用exceljs時報錯:no such file or directory

原來 tor 打印 contex 地址 nodejs 代碼 sta text

最近使用exceljs生成excel並保存時,總是失敗

 await workbook.xlsx.writeFile(tep)
                .then(function () {
                    context.result = {
                        state: true,
                        url: `算法生成的隨機名稱.xlsx`
                    }
                    //返回xlsx文件的下載位置
                    return
context }, function (error) { console.log(error) });

在打印的error中,總是說no such file or directory,把打印的地址拿出來訪問確實訪問不了,發現原來是系統環境問題

原來是在linux系統和windows系統中,地址是(linux)斜杠和(win)反斜杠分開的,因此在代碼中進行兼容性處理,即用\\來表示\,我的項目環境是Nodejs,驗證通過!!

         //linux環境
            let tep = process.cwd() + `/public/uploads/exp/${match[0] ? match[0].code : ""}_${yyy}.xlsx`;

            
// windows環境 let tep = process.cwd() + ‘\\src\\public\\uploads\\exp\\‘ + (match[0] ? match[0].code : ‘‘) + "_" + yyy + ‘.xlsx‘;

你的問題,也可能是:https://stackoverflow.com/questions/34811222/writefile-no-such-file-or-directory

使用exceljs時報錯:no such file or directory