Nome do input File

Modificar dinamicamente nome do input do tipo file

by Rodrigo Portillo

HTML

<input type="file" id="upload"/>




<br/><br/><br/>
<a href="https://velhobit.com.br/" target="_blank">Acesse VelhoBit.com.br</a>

JavaScript

var nomeQueQuero = "oimundo.sardinha";

function FileListItem(a) {
  a = [].slice.call(Array.isArray(a) ? a : arguments)
  for (var c, b = c = a.length, d = !0; b-- && d;) d = a[b] instanceof File
  if (!d) throw new TypeError("expected argument to FileList is File or array of File objects")
  for (b = (new ClipboardEvent("")).clipboardData || new DataTransfer; c--;) b.items.add(a[c])
  return b.files
}

var files = [
  new File(['content'], nomeQueQuero)
];


document.querySelector("#upload").files = new FileListItem(files);

//Se não for atualizar, aconselho retirar o arquivo antes de fazer um submit, ou substituir por valores identificáveis no back.