卷曲
正如您在curl manpage 上看到的,此表单的上传是通过指定数据字符串来完成的,并且可以直接从具有--data-binary @/path/to/file 语法的文件中完成:
--data-binary <data>
(HTTP) This posts data exactly as specified with no extra processing whatsoever.
If you start the data with the letter @, the rest should be a filename. Data is
posted in a similar manner as --data-ascii does, except that newlines and car‐
riage returns are preserved and conversions are never done.
If this option is used several times, the ones following the first will append
data as described in -d, --data.
如果图像仅在您的语言中以二进制字符串的形式提供,例如作为 Node.js 缓冲区,并且您不想访问文件系统,那么您可能必须通过将其包含在 @987654326 中来转义它@ 字符并将字符串中的每个' 字符替换为适当的转义序列,例如'\'',或者,如果这让您感到不安,则使用'"'"'。 (回想一下,echo 'abc'"def"'ghi' 会将 abcdefghi 回显为一个单元。)
Node.js
Node 更宽容一些,因为它有一个明确的缓冲区类型,但它确实需要更多的构造才能使其工作。在这里,我将返回数据包装在 Promise 中,以备不时之需:
const http = require("http");
function upload(image_buffer, image_id) {
return new Promise((accept, reject) => {
let options = {
method: "PUT",
hostname: "maven.nabisco.com",
port: 80,
path: "/artifactory/cdt-repo/folder/" + image_id,
headers: {
"Content-Type": "application/octet-stream",
"Content-Length": image_buffer.length
}
};
let data = [];
let request = http.request(options, response => {
response.on("data", chunk => data.push(chunk));
response.on("end", () =>
accept({
headers: response.headers,
statusCode: response.statusCode,
data: Buffer.concat(data)
})
);
});
request.on("error", err => reject(err));
request.write(image_buffer);
request.end();
});
}