来自Busboy的奇怪行为

我和Busboy有个奇怪的问题。 我通过使用Invoke-RestMethod来从PowerShell上传文件到Node.js中编写的远程服务器。 如果我使用streamfunction,代码工作没有任何问题。 它接受二进制数据并将文件写入本地驱动器而不会打嗝。 然而,当我使用Busboy时,它给了我“缺less边界错误”。 为了解决这个问题,我将边界传递给了Invoke-RestMethod。 这摆脱了边界的错误,但Busboy根本不会启动文件事件。 我一直在挠头,试图弄清楚已经两天了,解决scheme似乎没有了我。 几个星期前,这个代码工作得很好,但现在已经不复存在了。 我不确定工作环境是否有变化,但很奇怪。

stream代码:这工作得很好

服务器代码

fs = require('fs'); server = restify.createServer(); server.post('/file',restify.queryParser(),uploadFile); function uploadFile(req, res, next) { var wstream = fs.createWriteStream("x.jpg"); req.pipe(wstream); } 

电源shell

 $upload= Invoke-RestMethod -Uri "http://localhost:8088/file" -Method Post -InFile $imagePath -ContentType 'multipart/form-data' 

商店代码:这会引发Missing Boundary错误

服务器代码

 fs = require('fs'); server = restify.createServer(); server.post('/file',restify.queryParser(),uploadFile); function uploadFile(req, res, next) { var fileStream = new BusBoy({ headers: req.headers }); } 

电源shell

 $upload= Invoke-RestMethod -Uri "http://localhost:8088/file" -Method Post -InFile $imagePath -ContentType 'multipart/form-data' 

PowerShell代码与边界设置和修改Node.js代码。 “文件”不会被调用。

电源shell

 $begin = @" ---BlockBoundary--- "@ $end = @" ---BlockBoundary--- "@ Add-Content 'RequestBodySavedToFile' $begin $imageData = Get-Content $imagePath -Raw -Encoding Byte Add-Content 'RequestBodySavedToFile' $imageData -Encoding Byte Add-Content 'RequestBodySavedToFile' $end $url = "http://localhost:8088/file" $contentType = "multipart/form-data; boundary=$begin" $upload= Invoke-RestMethod -Uri $url1 -Method Post -InFile "RequestBodySavedToFile" -ContentType $contentType 

服务器代码

 fs = require('fs'); server = restify.createServer(); server.post('/file',restify.queryParser(),uploadFile); function uploadFile(req, res, next) { var fileStream = new BusBoy({ headers: req.headers }); req.pipe(fileStream); fileStream.on('file', function(fieldname, file, filename, encoding, mimetype) { console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype); res.end(); }); } 

任何想法是什么造成这个? 我很欣赏所有的input。

没有file事件的原因是因为根据multipart/form-data请求数据格式不正确(至less你错过了每个部分的适当头文件)。