简体   繁体   中英

How to rename a file in javascript

According to the documentation the way to go about it is:

new File(bits, name[, options]);  

My approach so far doesn't have any effects:

let input = document.getElementById('input');
let myFile = input[0];

var reader = new FileReader();
var fileByteArray = [];

reader.readAsArrayBuffer(myFile);
reader.onloadend = function (evt) {
    if (evt.target.readyState == FileReader.DONE) {
        var arrayBuffer = evt.target.result,
        array = new Uint8Array(arrayBuffer);
        for (var i = 0; i < array.length; i++) {
            fileByteArray.push(array[i]);
        }

        var f = new File(fileByteArray, "myNewFileName" + '.jpg', { type: revFile.type, lastModified: new Date().now() });
    }
}  

What is it that I am missing?

Thank you all in advance.

Could this perhaps be what you are looking for:

var input = document.querySelector("input[id=file-input]");
let myFile = input.files[0];

var copiedFile = new File([myFile], "myNewFileName" + ".jpg", {
  type: "image/jpeg",
  lastModified: new Date(),
});

CodePen link: https://codepen.io/kristofvdj88/pen/xxZRRXL?editors=1111

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM