0
嘗試允許用戶將圖像文件上傳到MEAN Stack應用程序中的Node.js服務器。我正在使用ng-file-upload作爲客戶端角度指令。這似乎工作得很好。當我將圖像傳遞到服務器時遇到錯誤。將文件讀取到磁盤錯誤,「setHeader()需要名稱和值」
我使用API路由來處理服務器端的工作。服務器將負責使用node-multiparty模塊將文件保存到磁盤。它似乎擊中路線,但當它試圖發出關閉事件時,我得到錯誤。 「需要‘名’和‘價值’爲的setHeader()。」 拋出新的錯誤(
文件我想在我的temp文件夾,但它不會保存到目標目錄我的服務器上加我得到的文件後,頭錯誤應該已經保存。所以我需要停止錯誤並fs.rename()
將文件保存到目標圖像目錄。
這裏是破譯密碼。
文件API .js
// router to save images
router.route('/img/upload')
.post(function (req, res) {
console.log("image upload hits the router")
var options = {};
var count = 0;
var form = new multiparty.Form(options);
//save file to disk
form.on('file', function (name, file) {
var uploadDirectory = 'img/user/profile/';
var oldPath = file.path;
var newPath = uploadDirectory + file.originalFilename;
fs.rename(oldPath, newPath, function (err) {
if (err) throw err;
console.log('renamed complete');
});
});
// Close emitted after form parsed
form.on('close', function() {
console.log('Upload completed!');
res.setHeader('text/plain'); // Here is the line that gives an error.
res.end('Received ' + count + ' files');
});
// Parse req
form.parse(req);
});