MiF
MiF

Reputation: 678

In FF45 FormData with empty file input didnt set Content-Type

Bugs started when clients had migrating to FF 45. They tryed to submit forms with empty file inputs and it fail. I detected that, if you submit form by AJAX with FormData, and it have empty file input - FF will not set Content-Type: application/octet-stream

I created minimal script to demonstrate this bug:

var fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.name = 'fileData';

var form = document.createElement('form');
form.method = 'post';
form.enctype = 'multipart/form-data';
form.appendChild(fileInput);

var xhr = new XMLHttpRequest();
xhr.open("POST", "/url");
xhr.send(new FormData(form));

I use standart FF45 on windows.

Upvotes: 2

Views: 681

Answers (1)

MiF
MiF

Reputation: 678

I didnt find any information about this bug, and use hack: delete empty file inputs before submited id:

var fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.name = 'fileData';

var form = document.createElement('form');
form.method = 'post';
form.enctype = 'multipart/form-data';
form.appendChild(fileInput);

// Filter empty file inputs
var childNodes = form.querySelectorAll('input[type=file]');
for (var i=0;i<childNodes.length;i++) {
  if (childNodes[i].files.length === 0) {
    childNodes[i].parentElement.removeChild(childNodes[i]);
  }
}

var xhr = new XMLHttpRequest();
xhr.open("POST", "/url");
xhr.send(new FormData(form));

Upvotes: 1

Related Questions