现在有人可能会认为所有其他浏览器都违反了规范,但这是错误的:因为规范声明:“may”(它没有说“must”)“..使用 value 属性作为初始文件名”。
而且,如果浏览器不接受设置文件输入值(也就是该值只是“只读”的),那么浏览器也不需要弹出这样一个“可怕”和“困难”的安全弹出窗口(如果用户不理解它(和/或被“训练”始终单击“确定”),那么它甚至可能无法起到作用)。
这是一个现实的安全/隐私风险:恶意网站(所有者/利用者)可以获得用户主目录的路径(其中个人资料、帐户、cookie、注册表用户部分、历史记录、收藏夹、桌面等位于已知常量位置),当典型的非技术 Windows 用户从 C:\Documents and Settings\[用户名]\My Documents\My Pictures\kinky_stuff\image.ext 上传文件时。
我甚至没有谈到在传输数据时的风险(即使通过https“加密”)或“安全”存储此数据!
For historical reasons, the value IDL attribute prefixes the filename
with the string "C:\fakepath\". Some legacy user agents actually
included the full path (which was a security vulnerability). As a
result of this, obtaining the filename from the value IDL attribute in
a backwards-compatible way is non-trivial. The following function
extracts the filename in a suitably compatible manner:
functionextractFilename(path) {
if (path.substr(0, 12) == "C:\\fakepath\\")
return path.substr(12); // modern browservar x;
x = path.lastIndexOf('/');
if (x >= 0) // Unix-based pathreturn path.substr(x+1);
x = path.lastIndexOf('\\');
if (x >= 0) // Windows-based pathreturn path.substr(x+1);
return path; // just the filename
}
functionextractFilename(s){
// returns string containing everything from the end of the string // that is not a back/forward slash or an empty string on error// so one can check if return_value===''return (typeof s==='string' && (s=s.match(/[^\\\/]+$/)) && s[0]) || '';
}
functiondrop(e) {
e.stopPropagation();
e.preventDefault();
var dt = e.dataTransfer;
var files = dt.files;
handleFiles(files);
}
Here, we retrieve the dataTransfer field from the event, then pull the
file list out of it, passing that to handleFiles(). From this point
on, handling the files is the same whether the user used the input
element or drag and drop.
var myBlobURL=window.URL.createFor(object); 返回一个Blob URL,在第一次使用后会自动撤销。
var myBlobURL=window.URL.createObjectURL(object, flag_oneTimeOnly); 返回可重复使用的Blob URL(除非flag_oneTImeOnly评估为true),并且可以使用window.URL.revokeObjectURL(myBlobURL)进行撤销。