分段很复杂,如果要使其看起来像客户端通常如何处理“分段/表单数据”,则必须做一些事情。首先,您必须选择一个边界键,这通常是一个随机字符串来标记各个部分的开始和结束(在这种情况下,由于您要发送单个文件,因此它只是一个部分)。每个部分(或一个部分)都将需要一个标头(由边界键初始化),设置内容类型,表单字段的名称和传输编码。零件完成后,您需要使用边界键标记每个零件的末端。
我从来没有从事过multipart的工作,但是我认为这是可以做到的。如果我错了,请有人纠正我:
var boundaryKey = Math.random().toString(16); // random stringrequest.setHeader('Content-Type', 'multipart/form-data; boundary="'+boundaryKey+'"');// the header for the one and only part (need to use CRLF here)request.write( '--' + boundaryKey + 'rn' // use your file's mime type here, if known + 'Content-Type: application/octet-streamrn' // "name" is the name of the form field // "filename" is the name of the original file + 'Content-Disposition: form-data; name="my_file"; filename="my_file.bin"rn' + 'Content-Transfer-Encoding: binaryrnrn' );fs.createReadStream('./my_file.bin', { bufferSize: 4 * 1024 }) // set "end" to false in the options so .end() isnt called on the request .pipe(request, { end: false }) // maybe write directly to the socket here? .on('end', function() { // mark the end of the one and only part request.end('--' + boundaryKey + '--'); });再说一次,我以前从未做过,但是我 认为 这是可以实现的。也许知识渊博的人可以提供更多的见解。
如果要以base64或原始二进制文件以外的其他编码形式发送它,则必须自己进行所有管道传递。最终将变得更加复杂,因为您将不得不暂停读取流并等待请求中的流失事件,以确保您不会耗尽所有内存(如果它不是一个大文件,通常不必为此担心)。
编辑: 实际上,没关系,您 可以 在读取流选项中设置编码。
如果还没有Node模块已经做到这一点,我会感到惊讶。也许有人对此主题有更深入的了解可以为您提供一些底层的细节方面的帮助,但是我认为应该在某个地方有一个模块来执行此操作。



