Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force binary encoding for multipart file/buffer fields #405

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to keep compatibility, this action may be controlled by parameters

post(
    "server/url",
    {
        file: {  buffer, filename, content_type, force_binary }
    },
    {
         multipart: true
    }
)


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

} else {
Expand Down