Javascript 更改输入类型文件选择的名称文件

Javascript 更改输入类型文件选择的名称文件,javascript,jquery,Javascript,Jquery,我想更改由input type=file选择的name文件,但它不起作用 这是我的代码: $(“文档”).ready(函数(){ $('body')。在('change','#FileID',function()上{ var name=document.getElementById('FileID'); name.files.item(0).name=Math.floor(Math.random()*100000); console.log('所选文件:'+name.files.item(0.

我想更改由
input type=file
选择的name文件,但它不起作用

这是我的代码:

$(“文档”).ready(函数(){
$('body')。在('change','#FileID',function()上{
var name=document.getElementById('FileID');
name.files.item(0).name=Math.floor(Math.random()*100000);
console.log('所选文件:'+name.files.item(0.name));
});
});

如果有人试图删除文件名,请尝试将其转换为base64。这样,它就不会有附加的名称,你可以上传你想要的。为此,我将留下一个使用reactJS的代码示例

1:以下是使用我们的函数调用onChange事件的输入文件类型:

<input onChange={this.handleBackgroundImagePreview} type="file" accept="image/png,image/gif,image/jpeg"></input>

无法从JS更改文件名。假设您的目标是在服务器上以不同的名称保存文件,那么在发送请求时,您必须对服务器上收到的副本执行此操作。您不能使用js更改文件名。如果使用
js
更改文件名,可能会导致在提交表单后生成错误
(找不到文件或类似的错误)
。您可以在上载到目标文件夹时重命名文件,具体取决于您使用的编程语言。问题:为什么要更改
input type=file
的文件名?你想通过改变这一点来实现什么?听起来像是一个错误,它被阻止的原因是为了安全,阻止恶意网站做如下事情:-将文件设置为c:\password.txt-上传文件-在一个隐藏的iframe中。@raptor,重命名的目的是什么?顺便说一句,也可以在服务器端重命名文件。此链接可能会帮助您:
handleBackgroundImagePreview = (e) =>{
  // Retrieves the selected Image or file
  const file = e.target.files[0]

  //Calling async file reader with a callback
  let fileBase64 = '';
  this.getBase64(file, (result) => {
      fileBase64 = result;
      console.log(fileBase64)
      //In here you could upload to the server the base 64 data as you want
  });
}

getBase64(file, cb) {
  let reader = new FileReader();
  reader.readAsDataURL(file);
  reader.onload = function () {
      cb(reader.result)
  };
  reader.onerror = function (error) {
      console.log('Error: ', error);
  };
}