有没有办法使用 JavaScript 获取正在上传的文件的实际创建/修改时间?
对于PHP,使用filectime()
和filemtime()
,它只显示文件上传的日期/时间,而不显示文件实际创建/修改的时间在来源上。
简而言之,我想要的是在上传之前/期间/之后检查文件的 m-time(只要可能),并决定是否将文件存储在服务器上,并将相同的情况报告给客户端。
Is there ever a way possible to get the actual creation / modification time of the file being uploaded, using JavaScript?
As for PHP, using filectime()
and filemtime()
, it only shows the date / time the file is uploaded, and not the time the file is actually created / modified on the source.
In short, what I want is to check the m-time of a file before/during/after upload (where-ever possible) and decide whether or not to store the file on the server, and report the same back to the client.
发布评论
评论(3)
如果您谈论的是用户计算机上的文件日期/时间,您可以通过 文件 API(支持),它提供
lastModified
,这是日期/时间为自纪元以来的毫秒数(如果您想要一个Date
,您可以将其传递给new Date
)。 (还有已弃用的lastModifiedDate
,但是文件 API 在现代浏览器中得到普遍支持(您要使用的特定功能是File
对象)。您可以从File
对象获取值,并将该信息包含在单独的(例如,隐藏的)字段中。以下是读取上次修改日期的一个粗略但完整的示例(live copy)
:您无法从服务器上上传的文件获取时间的原因是请求中仅传输文件的内容,而不是客户端的文件系统元数据。
If you're talking about the file date/time on the user's machine, you can get that via the File API (support), which provides
lastModified
, which is the date/time as a number of milliseconds since The Epoch (if you want aDate
, you can pass that intonew Date
). (There's also the deprecatedlastModifiedDate
, but that is deprecated and not supported on Safari [at least].) The File API is universally supported in modern browsers (the particular feature you'd be using is theFile
object). You'd get the value from theFile
object and include that information in a separate (for instance, hidden) field.Here's a rough-but-complete example of reading the last modified date (live copy):
The reason you couldn't get the time from the uploaded file on the server is that only the content of the file is transmitted in the request, not the client's filesystem metadata.
JavaScript 无法访问本地文件系统,因此如果不使用 Flash、Java 或 Active-x,则无法获取此信息。
JavaScript does not have access to the local filesystem, so you can't get to this information without using Flash, Java or Active-x.
也许您可以使用 javascript 获取上次修改时间,然后在其他 javacript 中使用该时间进行排序。这次将是格林威治标准时间。
Perhaps you could use javascript to get the last modified time, then use that in some other javacript to sort on that. This time will be in GMT.