"未定义"访问File对象的某些列出属性时返回 [英] "undefined" returned when accessing some listed properties of File object

查看:144
本文介绍了"未定义"访问File对象的某些列出属性时返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法访问对象的宽度或高度键。

I can't seem to access the width or height keys of my object.

我正在使用dropzone.js,它有一个addFile事件,返回文件和第一个参数。

I'm using dropzone.js which has an event for addedFile which returns the file and the first param.

所以:

var myDropzone = new Dropzone('#dropzone', {url: '/'});

myDropzone.on('addedFile', function(file) {
    console.log(file);
});

回调工作正常,在我的控制台中我看到:

The callback works just fine, in my console I see:

正如您所看到的,显然高度和放大器;宽度键可用。

As you can see, there's clearly the height & width keys available.

myDropzone.on('addedFile', function(file) {
    console.log(file.name); // returns the whole strong
    console.log(file.width); // returns undefined
    console.log(file['width']);  // returns undefined
});

下面是截图:

我的问题是,为什么是名字可用,但没有宽度或高度?是因为他们是只读还是什么?如果是这种情况,是否可以访问它?

My question is, why is name available, but not width or height? Is it because they're readonly or something? If that's the case, is it even possible to access it?

推荐答案

File.width property是一个DropzoneJS扩展,并不是核心的一部分文件API ;它是稍后添加的

The File.width property is a DropzoneJS extension and is not a part of the core File API; it is added later.


Dropzone将数据添加到文件对象中,您可以使用 事件开火。您可以访问 file.width file.height 如果它是图像..

Dropzone adds data to the file object you can use when events fire. You can access file.width and file.height if it's an image..

如果适用图像大小信息时间 thumbnail 事件发生。 保证在此事件之前设置。

If applicable the image size information is made available by the time the "thumbnail" event occurs. It is not guaranteed to be set before this event.

文档不是很明确,仅暗示缩略图何时有已生成,但这是来源的行为 (请参阅createThumbnail / resize函数) - 生成缩略图时收集图像大小

The documentation isn't very clear on this only alluding to "when the thumbnail has been generated", but such is the behavior of the source (see the createThumbnail/resize functions) - the image size is collected when the thumbnail is generated.

可以看到初始行为,因为 console.log (在浏览器中,例如Chrome,对待它类似于 console.dir )显示实时对象。这反过来给了异步缩略图生成和相关图像维度收集足够的时间,以便在浏览器在控制台中显示对象的现在分配的属性之前完成。 (这也解释了为什么使用超时读取属性值有效 - 即使这样是一种可靠的方法。)

The initial behavior is seen because console.log (in browsers, eg. Chrome, that treat it similar to console.dir) displays the "live" object. This in turn has given enough time for the asynchronous thumbnail generation, and associated image dimension gathering, to complete before the browser displays the object's now-assgined properties in the console. (This also explains why using a timeout to read the property value works - even though such is not a reliable approach.)

另一方面手,直接访问 file.width 强制立即评估仍未设置的属性,这导致 undefined addedFile回调中。

On the other hand, directly accessing the file.width forces immediate evaluation of the still-not-set property, which results in undefined in the "addedFile" callback.

这篇关于"未定义"访问File对象的某些列出属性时返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆