问题描述
从这个答案,我感觉到 multipart / form-data 。但是,如果我的用例是从服务器上传文件到服务器,如果我把 Content-Type 作为 application / octet-stream 甚至是特定的类型 image / png
我正在node.js服务器上进行实验,并使用获取文件内容,并使用REST库进行HTTP POST。但是我对读取文件时使用的编码有点困惑。被默认的fs.readFile返回Buffer对象。我试图使用二进制但是文档它是二进制文件被弃用,以支持缓冲区对象。在这里阅读文件的内容和上传的正确方法是什么?这可以做到没有 multipart / form-data
回答这个,因为我发现我是寻找。
@npcode是正确的。我不需要指定任何编码,我可以直接使用Buffer对象,我得到它的工作。
我现在使用,它将Buffer对象放在主体中。我的困惑起源于我最初使用的其他客户端,它是
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的文件(服务器到服务器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!