简体   繁体   English

如何从JavaScript中的File-API对象中删除属性?

[英]How to remove a property from a File-API object in JavaScript?

I'm using the JavaScript "file" api to read information about images that will be uploaded, in order to generate a preview thumbnail before the actual upload occurs, based on a demo found here ( JavaScript file api ). 我正在使用JavaScript“文件”api来读取有关将要上传的图像的信息,以便在实际上传发生之前根据此处的演示( JavaScript文件api )生成预览缩略图。

Using an file input with a "multiple" attribute, one or more files can be selected at the same time, like so: <input type="file" id="imageFiles" name="userfile[]" multiple /> 使用具有“多个”属性的文件输入,可以同时选择一个或多个文件,如下所示: <input type="file" id="imageFiles" name="userfile[]" multiple />

Once the image(s) have been selected by the user, I inspect the element with firebug and I can see in the DOM that the "files" object contains information about each image selected. 一旦用户选择了图像,我用firebug检查元素,我可以在DOM中看到“files”对象包含有关所选每个图像的信息。

The "file" object will contain as many properties as files were selected and each property has information about each image. “文件”对象将包含与选择文件一样多的属性,每个属性都包含有关每个图像的信息。 I could only describe the "file" object as an associative array in the sense that each property is an object in itself. 我只能将“文件”对象描述为关联数组,因为每个属性本身就是一个对象。

What I want to do is be able to remove from the "file" object a given proprety, thus removing one of the images that will be uploaded, without having to select the images again. 我想要做的是能够从“文件”对象中删除给定的属性,从而删除将要上传的图像之一,而无需再次选择图像。

So far this is what I've tried and it does not work: 到目前为止,这是我尝试过的,它不起作用:

var getAllFiles = document.getElementById('imageFiles');
var allImages = getAllFiles.files; // returns an object with X properties
delete allImages[X]; // "X" is whatever property key I'm passing in

I've tried the "splice()" method but it does not work either. 我已经尝试了“splice()”方法,但它也不起作用。

Thank you very much. 非常感谢你。

The .files is readonly so you cannot modify it. .files是只读的,因此您无法修改它。 You can extract the files themselves to an array but then you cannot use the normal form upload, you'd need to upload them through binary XHR 您可以将文件本身提取到一个数组,但是你不能使用普通的表单上传,你需要通过二进制XHR上传它们

You can extract the files to a normal array using: 您可以使用以下方法将文件解压缩到普通数组:

var files = [].slice.call( this.files );
files.splice( 1, 1 ); //whatever works for normal array

You could also just put accept="image/*" in the input in the first place: 您也可以在输入中首先放置accept="image/*"

<input type="file" id="imageFiles" accept="image/*" name="userfile[]" multiple />

I take it you understand these won't work in IE or any other browser without file API. 我认为你理解这些在IE或没有文件API的任何其他浏览器中都不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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