英文:
not able to modify javascript object which i get from input tag
问题
我正在尝试在我的电商网站中添加上传功能,在我的HTML中我创建了以下代码:
`<input type="file" id="file" accept=".jpg, .png, .jpeg, .svg, .jfif, pjpeg, pjp" multiple><br>`,它会给我一个包含文件的对象,我试图在这个对象中修改文件名,但未能成功,请帮忙
**HTML**
<form id="form">
<input type="text" id="title" placeholder="产品名称"><br>
<input type="number" id="price" placeholder="价格"><br>
<textarea id="description" placeholder="描述" name="description" rows="4" cols="50"></textarea><br>
<input type="file" id="file" accept=".jpg, .png, .jpeg, .svg, .jfif, pjpeg, pjp" multiple><br>
<button type="submit">上传文件</button>
</form>
**JS**
const button = document.querySelector("form").addEventListener('submit',(e)=>{
e.preventDefault();
let userFiles = document.getElementById("file").files;
let i = 1;
for (let element of userFiles) {
// console.log(element.type)
let extension = element.type.slice(6,element.type.length+1);
console.log("user_file"+i+"."+extension);
element.name = "user_file"+i+"."+extension;
console.log(element["name"]);
i++;
}
});
我想要以“user_file`i`.extension”的格式更改上传文件的名称。
英文:
i am trying to make upload functionality in my e-commerce website and in my html i have created
<input type="file" id="file" accept=".jpg, .png, .jpeg, .svg, .jfif, pjpeg, pjp" multiple><br>
which gives me a object of items and i am trying to modify the files name within this object but couldn't able to do that please help
HTML
<form id="form">
<input type="text" id="title" placeholder="Product Name"><br>
<input type="number" id="price" placeholder="Price"><br>
<textarea id="description" placeholder="Description" name="description" rows="4" cols="50"></textarea><br>
<input type="file" id="file" accept=".jpg, .png, .jpeg, .svg, .jfif, pjpeg, pjp" multiple><br>
<button type="submit">Upload File</button>
</form>
JS
const button = document.querySelector("form").addEventListener('submit',(e)=>{
e.preventDefault();
let userFiles = document.getElementById("file").files;
let i = 1;
for (let element of userFiles) {
// console.log(element.type)
let extension = element.type.slice(6,element.type.length+1);
console.log("user_file"+i+"."+extension);
element.name = "user_file"+i+"."+extension;
console.log(element["name"]);
i++;
}
});
i want to change the name of uploaded files in the format "user_filei
.extension"
答案1
得分: 2
HTMLInputElement的
files属性返回一个包含
File对象和
File对象的所有属性都是只读的
FileList`对象。
参见:
MDN FileList
MDN File
英文:
The files
property of the HTMLInputElement
's interface returns a FileList
object containing File
objects and all properties of a File
object are read-only, they therefor cannot be changed.
See:
MDN FileList
MDN File
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论