上传没有multipart / form-data的文件(服务器到服务器) [英] Uploading file without multipart/form-data (server to server)

查看:891
本文介绍了上传没有multipart / form-data的文件(服务器到服务器)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个答案 https://stackoverflow.com/a/1695287/256400 ,我感觉到<从浏览器上传文件到服务器需要使用strong> multipart / form-data 。但是,如果我的用例是从服务器上传文件到服务器,如果我把 Content-Type 作为 application / octet-stream 甚至是特定的类型 image / png



我正在node.js服务器上进行实验,并使用 fs.readFile 获取文件内容,并使用REST库进行HTTP POST。但是我对读取文件时使用的编码有点困惑。被默认的fs.readFile返回Buffer对象。我试图使用二进制但是文档这里说它是二进制文件被弃用,以支持缓冲区对象。在这里阅读文件的内容和上传的正确方法是什么?这可以做到没有 multipart / form-data

解决方案

回答这个,因为我发现我是寻找。



@npcode是正确的。我不需要指定任何编码,我可以直接使用Buffer对象,我得到它的工作。

我现在使用请求npm模块,它将Buffer对象放在主体中。我的困惑起源于我最初使用的其他客户端,它是 mojito平台休息客户端


From this answer https://stackoverflow.com/a/1695287/256400 , I get the feeling that multipart/form-data is needed to upload file from browser to server. But if my use case is to upload file from server to server shouldn't it work if I put the Content-Type as application/octet-stream or even specific types like image/png

I was experimenting with this on a node.js server and used fs.readFile to get the file content and used a REST library to do HTTP POST. But I am a bit confused on the encoding to be used when reading the file. Be default fs.readFile returns Buffer object. I was tempted to use binary But documentation here says it's binary is deprecated in favor of Buffer Objects. What would be the correct approach here to read the content of a file and upload? Can this be done without multipart/form-data

解决方案

Answering this since I found what I was looking for.

@npcode is correct. I didn't need to specify any encoding and I was able to work directly with Buffer object and I got it working.

I am now using request npm module which takes Buffer objects in body. My confusion arised from the rest client I was using initially which was mojito platforms rest client

这篇关于上传没有multipart / form-data的文件(服务器到服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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