How to change javascript file name from input = File

I need to change the file name (not the file, only the name metadata) when uploading to the sharepoint site.

I realized that it would be easy enough to change the html attribute in javascript, rather than playing with the Sharepoint backend. So when I upload a file, it changes the name of the file (not the data)

something like that:

function PreSaveAction(){
   var file = document.GetElementById('fileupload1');
   file.files[0].name='ChangedName.tmp'

return true;
}

Is this impossible due to the nature of the attributes of the blocked entry = 'file'?

+6
source share
2 answers

try it:

var file = document.GetElementById('fileupload1');
var blob = file.files[0].slice(0, file.files[0].size, 'image/png'); 
newFile = new File([blob], 'name.png', {type: 'image/png'});

note: this is for an image type, you must change this type with the type that you are actually using.

+15
source

A simpler and more efficient approach to memory - change the file name property to writeable:

Object.defineProperty(fileToAmend, 'name', {
  writable: true,
  value: updatedFileName
});

fileToAmend - , updatedFileName - .

'' '[ Object]'

0

All Articles