#修改configure爲false的對象屬性 #修改File對象的name屬性[code snippet]

handelfileChange(e) { this.file = null; //init const FD = new FormData(); let newNameFile = this.rewriteFileName(e.target.files[0]); FD.append("mFile", newNameFile); this.file = FD; }, rewriteFileName(fileObj) { /* 後臺在處理某文件沒處理完時,前臺再次傳一個同名文件, 後臺就會報錯。 所以解決的方案就是每次都需要修改文件名,這個處理可以後臺處理。 這裏是在前臺處理 */ /* file 對象中的name 屬性,原生是隻讀屬性, 以下過程是通過新建一個變量,然後用name的getter訪問這個變量, 就可以達到"改名"的目的 */ Object.defineProperties(fileObj, { xname: { value: // prettier-ignore new Date().getTime() +"." +fileObj.name.split(".").at(-1) //at(-1) 用於取得最後一個元素 }, name: { get: function() { // 複寫name 的原始get方法 return this.xname; } } }); return fileObj; },
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章