Skip to content

Commit

Permalink
Force binary encoding for multipart file/buffer fields
Browse files Browse the repository at this point in the history
Fixes tomas#403
  • Loading branch information
mciasuen committed May 9, 2022
1 parent e5dee92 commit cdd613a
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function generate_part(name, part, boundary, callback) {
var return_part = '--' + boundary + '\r\n';
return_part += 'Content-Disposition: form-data; name="' + name + '"';

function append(data, filename) {
function append(data, filename, force_binary) {

if (data) {
var binary = part.content_type.indexOf('text') == -1;
var binary = force_binary || part.content_type.indexOf('text') == -1;
return_part += '; filename="' + encodeURIComponent(filename) + '"\r\n';
if (binary) return_part += 'Content-Transfer-Encoding: binary\r\n';
return_part += 'Content-Type: ' + part.content_type + '\r\n\r\n';
Expand All @@ -55,11 +55,11 @@ function generate_part(name, part, boundary, callback) {
if ((part.file || part.buffer) && part.content_type) {

var filename = part.filename ? part.filename : part.file ? basename(part.file) : name;
if (part.buffer) return append(part.buffer, filename);
if (part.buffer) return append(part.buffer, filename, true);

readFile(part.file, function(err, data) {
if (err) return callback(err);
append(data, filename);
append(data, filename, true);
});

} else {
Expand Down

0 comments on commit cdd613a

Please sign in to comment.