Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/436.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用put方法使用缓冲区数组写入文件?_Javascript_Node.js_Stream_Buffer - Fatal编程技术网

Javascript 如何使用put方法使用缓冲区数组写入文件?

Javascript 如何使用put方法使用缓冲区数组写入文件?,javascript,node.js,stream,buffer,Javascript,Node.js,Stream,Buffer,是否有任何方法可以使用缓冲区数组和内容类型以及put方法来写入文件 requestify.request('some url', { method: 'PUT', body: buffArray, //need modifications here headers: { 'Content-Type': res_file.heade

是否有任何方法可以使用缓冲区数组和内容类型以及put方法来写入文件

requestify.request('some url', {
                    method: 'PUT',
                    body: buffArray, //need modifications here
                    headers: {
                        'Content-Type': res_file.headers['content-type']
                    }
                }).then(function (res) {
                    console.log(res);
                })
我可以发送数据,但文件存储方式不正确

工作Java代码

   httpcon.setRequestMethod("PUT");
        httpcon.setReadTimeout(100000);
        httpcon.setDoOutput(true);
        httpcon.setRequestProperty("Content-Type", conenttype);
        httpcon.connect();
        OutputStream os = httpcon.getOutputStream();

        os.write(in.toByteArray(), 0, in.size());

        responceCode = httpcon.getResponseCode();

        httpcon.disconnect();

我个人的建议是使用Node.JS中的内置http或https包

为什么??因为您想要写入和读取可能会给您带来问题的二进制文件,而对于我用requestify测试的内容,当您使用二进制响应时,它会给您带来问题,它会将它们字符串化

您可以简单地使用流,这将为您节省很多麻烦

您可以使用以下方法进行测试,例如:

常数fs=要求“fs”; consthttp=require'https'; const req=http.request{ 主机:“raw.githubusercontent.com”, 路径:'/smooth code/svgr/master/resources/svgr logo.png', 方法:“获取” },res=>{ createWriteStream'test.png'; }; 请求结束; 并根据您提供的代码进行调整:

常数fs=要求“fs”; consthttp=require'https'; const req=http.request{ 主持人:“某个主持人”, 路径:'/some/path', 方法:'放', 标题:{ “内容类型”:res_file.headers['Content-Type'] } },res=>{ createWriteStream'your-output-file.blob'; }; //这部分:如果来自HDD或其他请求,我建议也使用.pipe req.writebuffArray; 请求结束; 进一步资料:

http包


fs package

您的服务器如何处理请求?