使用节点http模块上传文件 [英] Uploading a file with node http module

查看:133
本文介绍了使用节点http模块上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将文件上传到具有节点http模块的远程服务器(并且没有第三方库)?

我尝试了以下方法,但是不起作用我得到的服务器上没有数据):

$ p $ 函数writeBinaryPostData(req,filepath){
var fs = require( 'FS');

var boundaryKey = Math.random()。toString(16); // random string
var boundaryStart =` - $ {boundaryKey} \r\\\
`,
boundaryEnd =`\r\\\
- $ {boundaryKey} - `,
contentDispositionHeader ='Content-Disposition:form-data; name =filefilename =file.txt\r\\\
',
contentTypeHeader ='Content-Type:application / octet-stream \r\\\
',
transferEncodingHeader = 'Content-Transfer-Encoding:binary \r\\\
';

var contentLength = Buffer.byteLength(
boundaryStart +
boundaryEnd +
contentDispositionHeader +
contentTypeHeader +
transferEncodingHeader
)+ fs.statSync(文件路径).size;

var contentLengthHeader =`Content-Length:$ {contentLength} \r\\\
`;

req.setHeader('Content-Length',contentLength);
req.setHeader('Content-Type','multipart / form-data; boundary ='+ boundaryKey);

req.write(
boundaryStart +
contentTypeHeader +
contentDispositionHeader +
transferEncodingHeader
);

fs.createReadStream(filepath,{bufferSize:4 * 1024})
.on('data',(data)=> {
req.write(data) ;
。)('end',()=> {
req.write(boundaryEnd);
req.end();
} );


解决方案

以下代码:

pre $函数writeBinaryPostData(req,filepath){
var fs = require('fs'),
data = fs.readFileSync(filepath);

var crlf =\r\\\

boundaryKey = Math.random()。toString(16),
boundary =` - $ {boundaryKey} `,
delimeter =`$ {crlf} - $ {boundary}`,
headers = [
'Content-Disposition:form-data; NAME = 文件; filename =test.txt'+ crlf
],
closeDelimeter =`$ {delimeter} - `,
multipartBody;


multipartBody = Buffer.concat([
new Buffer(delimeter + crlf + headers.join('')+ crlf),
data,
新缓冲区(closeDelimeter)]
);

req.setHeader('Content-Type','multipart / form-data; boundary ='+ boundary);
req.setHeader('Content-Length',multipartBody.length);

req.write(multipartBody);
req.end();
}

根据我找到的代码这里


How do I upload a file to a remote server with the node http module (and no 3rd party libs)?

I tried the following but it's not working (I get no data on the server):

function writeBinaryPostData(req, filepath) {
    var fs = require('fs');

    var boundaryKey = Math.random().toString(16); // random string
    var boundaryStart = `--${boundaryKey}\r\n`,
        boundaryEnd = `\r\n--${boundaryKey}--`,
        contentDispositionHeader = 'Content-Disposition: form-data; name="file" filename="file.txt"\r\n',
        contentTypeHeader = 'Content-Type: application/octet-stream\r\n',
        transferEncodingHeader = 'Content-Transfer-Encoding: binary\r\n';

    var contentLength = Buffer.byteLength(
        boundaryStart +
        boundaryEnd +
        contentDispositionHeader +
        contentTypeHeader +
        transferEncodingHeader
    ) + fs.statSync(filepath).size;

    var contentLengthHeader = `Content-Length: ${contentLength}\r\n`;

    req.setHeader('Content-Length', contentLength);
    req.setHeader('Content-Type',  'multipart/form-data; boundary=' + boundaryKey);

    req.write(
        boundaryStart +
        contentTypeHeader +
        contentDispositionHeader +
        transferEncodingHeader
    );

    fs.createReadStream(filepath, { bufferSize: 4 * 1024 })
        .on('data', (data) => {
            req.write(data);
        })
        .on('end', () => {
            req.write(boundaryEnd);
            req.end();
        });
}

解决方案

I got it working with the following code:

function writeBinaryPostData(req, filepath) {
    var fs = require('fs'),
        data = fs.readFileSync(filepath);

    var crlf = "\r\n",
        boundaryKey = Math.random().toString(16),
        boundary = `--${boundaryKey}`,
        delimeter = `${crlf}--${boundary}`,
        headers = [
          'Content-Disposition: form-data; name="file"; filename="test.txt"' + crlf
        ],
        closeDelimeter = `${delimeter}--`,
        multipartBody;


    multipartBody = Buffer.concat([
        new Buffer(delimeter + crlf + headers.join('') + crlf),
        data,
        new Buffer(closeDelimeter)]
    );

    req.setHeader('Content-Type', 'multipart/form-data; boundary=' + boundary);
    req.setHeader('Content-Length', multipartBody.length);

    req.write(multipartBody);
    req.end();
}

based on code I found here

这篇关于使用节点http模块上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆