- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题在这里已经有了答案:
How to set file input value when dropping file on page? [duplicate]
(1 个回答)
3年前关闭。
Note:
The answer(s) below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element via JavaScript in 2017.
See the answer in this question for details as well as a demo:
How to set file input value programatically (i.e.: when drag-dropping files)?
最佳答案
好的,您想“记住并重新填充文件输入”,“记住他们的选择并使用重新加载页面时预先选择的文件重新显示文件输入”..
在对我之前的回答的评论中,您表示您对替代方案并不真正开放:“抱歉,没有 Flash 和小程序,只有 javscript 和/或文件输入,可能会拖放。”
我在浏览(相当多)重复问题时注意到( 1 、 2 、 3 等),几乎所有其他答案都在:“不,你不能遵循安全问题”,这是可选的通过一个简单的概念或代码示例概述安全风险。
然而,像骡子一样固执的人(在一定程度上不一定是坏事)可能会认为这些答案是:“不,因为我这么说”,这确实是不同的:“不,这里是规范禁止它”。
所以这是我第三次也是最后一次回答你的问题(我带你到水坑,我带你到河边,现在我推你到源头,但我不能让你喝)。
编辑 3:
您想要做的实际上曾经在 RFC1867 第 3.4 节中描述/“建议”:
The VALUE attribute might be used with
<INPUT TYPE=file>
tags for a default file name. This use is probably platform dependent. It might be useful, however, in sequences of more than one transaction, e.g., to avoid having the user prompted for the same file name over and over again.
User agents may use the value of the value attribute as the initial file name.
It is important that a user agent not send any file that the user has not explicitly asked to be sent. Thus, HTML interpreting agents are expected to confirm any default file names that might be suggested with
<INPUT TYPE=file VALUE="yyyy">
.
<input type="file" value="C:\foo\bar.txt>
或由 javascript 设置的值(
elm_input_file.value='c:\\foo\\bar.txt';
)。
may
”(它没有说“
must
”)“..使用值属性作为初始文件名” .
- The value IDL attribute is in mode filename.
...- The element's value attribute must be omitted.
The value IDL attribute allows scripts to manipulate the value of an input element. The attribute is in one of the following modes, which define its behavior:
On getting, it must return the string "C:\fakepath\" followed by the filename of the first file in the list of selected files, if any, or the empty string if the list is empty. On setting, if the new value is the empty string, it must empty the list of selected files; otherwise, it must throw an InvalidStateError exception.
must
抛出一个 InvalidStateError 异常”如果有人试图将文件输入值设置为非空字符串!!! (但可以通过将其值设置为空字符串来清除输入字段。)
alert( elm_input_file.value );
等 javascript 并且浏览器也发送了这个完整路径+文件名(+扩展名)到表单提交上的接收服务器。C:\Documents and Settings\[UserName]\My Documents\My Pictures\kinky_stuff\image.ext
。On getting, it must return the string "C:\fakepath\" followed by the filename of the first file in the list of selected files, if any, or the empty string if the list is empty.
This "fakepath" requirement is a sad accident of history
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:
function extractFilename(path) {
if (path.substr(0, 12) == "C:\\fakepath\\")
return path.substr(12); // modern browser
var x;
x = path.lastIndexOf('/');
if (x >= 0) // Unix-based path
return path.substr(x+1);
x = path.lastIndexOf('\\');
if (x >= 0) // Windows-based path
return path.substr(x+1);
return path; // just the filename
}
c:\fakepath\Some folder\file.ext
的文件(因为它会返回: Some folder\file.ext
)...function extractFilename(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]) || '';
}
c:\fakepath\
前置到文件名This specification defines an API to navigate file system hierarchies, and defines a means by which a user agent may expose sandboxed sections of a user's local filesystem to web applications. It builds on [FILE-WRITER-ED], which in turn built on [FILE-API-ED], each adding a different kind of functionality.
is no more actively maintained, the specification is abandonned for the moment as it didn't get any significant traction
This specification provides an API for representing file objects in web applications, as well as programmatically selecting them and accessing their data. This includes:
- A FileList interface, which represents an array of individually selected files from the underlying system. The user interface for selection can be invoked via
<input type="file">
, i.e. when the input element is in the File Upload state [HTML] .- A Blob interface, which represents immutable raw binary data, and allows access to ranges of bytes within the Blob object as a separate Blob.
- A File interface, which includes readonly informational attributes about a file such as its name and the date of the last modification (on disk) of the file.
- A FileReader interface, which provides methods to read a File or a Blob, and an event model to obtain the results of these reads.
- A URL scheme for use with binary data such as files, so that they can be referenced within web applications.
FileList
可以由文件模式下的输入字段填充: <input type="file">
。files
,它是一个类似数组的 FileList object
,它引用输入元素的用户选择的文件,并且可以被 FileList interface
访问files
-attribute of the type FileList
is read-only (File API section 5.2) ? :The HTMLInputElement interface [HTML] has a readonly attribute of type FileList...
The real magic happens in the drop() function:
function drop(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.
dataTransfer
属性有一个类似数组的属性 files
,它是一个类似数组的 FileList object
并且我们刚刚了解到 FileList9131141214124114141 ..
name
The name of the file; on getting, this must return the name of the file as a string. There are numerous file name variations on different systems; this is merely the name of the file, without path information. On getting, if user agents cannot make this information available, they must return the empty string.
lastModifiedDate
The last modified date of the file. On getting, if user agents can make this information available, this must return a new Date[HTML] object initialized to the last modified date of the file. If the last modification date and time are not known, the attribute must return the current date and time as a Date object.
size
属性:
F.size is the same as the size of the fileBits Blob argument, which must be the immutable raw data of F.
(elm_input||event.dataTransfer).files
给出了 FileList 对象。 (elm_input||event.dataTransfer).files.length
给出了文件的数量。 (elm_input||event.dataTransfer).files[0]
是选择的第一个文件。 (elm_input||event.dataTransfer).files[0].name
是选择的第一个文件的文件名value
)。 This specification defines a scheme with URLs of the sort:
blob:550e8400-e29b-41d4-a716-446655440000#aboutABBA.
URL store
(浏览器甚至应该有自己的迷你 HTTP 服务器,这样人们就可以在 css、img src 甚至 XMLHttpRequest 中使用这些 url。
Blob URL
s:
var myBlobURL=window.URL.createFor(object);
返回一个 Blob URL
,在第一次使用后会自动撤销。 var myBlobURL=window.URL.createObjectURL(object, flag_oneTimeOnly);
返回可重用的 Blob URL
(除非 flag_oneTImeOnly 评估为真)并且可以用 0x251812143133 撤销。 window.URL.revokeObjectURL(myBlobURL)
仅在 session 期间维护(因此它会在页面刷新时继续存在,因为它仍然是同一个 session )并且在卸载文档时丢失。
The object URL is a string identifying the File object. Each time you call window.URL.createObjectURL(), a unique object URL is created, even if you've created an object URL for that file already. Each of these must be released. While they are released automatically when the document is unloaded, if your page uses them dynamically, you should release them explicitly by calling window.URL.revokeObjectURL()
URL Store
字符串存储在 cookie 或持久性本地存储中,该字符串在新 session 中也无用!
关于javascript - 记住并重新填充文件输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20537696/
这个问题在这里已经有了答案: How does Scala's apply() method magic work? (3 个回答) 9年前关闭。 假设我在 scala 中有一个 MyList 类,其
这个问题在这里已经有了答案: What is a non-capturing group in regular expressions? (18 个回答) Reference - What does
这个问题是针对嵌入式系统的! 我有以下选项来初始化一个对象: Object* o = new Object(arg); 这会将对象放入堆中并返回指向它的指针。我不喜欢在嵌入式软件中使用动态分配。 Ob
我自己搜索过,没能成功的正则表达式。 我有一个 html 文件,其中包含 [] 之间的变量我想把每一个字都写进去。 [client_name][client_company] [cl
我是 Python 新手。我不明白为什么这段代码不起作用: reOptions = re.search( "[\s+@twitter\s+(?P\w+):(?P.*?)\s+]", d
在过去 7 个月左右的时间里,我几乎一直在使用 .NET C# 进行编程。在那之前,我的大部分编程都是用 C++(从学校里学的)。在工作中,我可能需要在接下来的几个月里做一大堆 C 语言。我对 C 的
我是 RE 的新手,我正在尝试获取歌词并分离出歌词标题、和声和主唱: 下面是一些歌词的例子: [Intro] D.A. got that dope! [Chorus: Travis Scott] Ic
这可能是不可能的,但我想检查是否可以用一种简单的方式表达这样的事情: // obviously doesn't work class Foo : IFoo where T: Bar {
我们的应用程序中有“user”和“study”实体,存储在它们各自的表中。一项研究代表一种研究和已收集的数据。它们是多对多的关系,所以我们需要一个链接表:studies_users。 我们为用户分配角
将测试条件添加到 Visual Studio 2010 数据库单元测试(对于 SQL Server 2008)时,这些条件称为例如rowCountCondition1、rowCountConditio
在模拟器上,我可以从设置中卸载 SD 卡。 然后我可以将它安装到我的操作系统上,然后正常卸载它。 我一直无法弄清楚如何在模拟器上重新安装它(无需重新启动)。 提示: adb 命令 remount 是无
假设在一个分支上执行了一系列提交,但该分支尚未与主干重新同步。是否可以从提交中生成全局补丁?是否可以从一系列提交中生成“分组”补丁?如果是,如何? 最佳答案 svn diff -rXXX:YYY UR
在某些情况下,我想在我的应用程序中锁定调整大小功能,为此我尝试对属性进行数据绑定(bind),并且不允许在某些情况下更改它,但没有成功。 有没有办法这样做? 这是我不成功的尝试: XAML: Vie
当我的计算机连接多个显示器时,我可以检测它们,并根据从获取的值设置位置来向它们绘制图形 get(0, 'MonitorPositions') 但是,当我在 MATLAB 运行时断开监视器时,此属性不会
我们有一个grails应用程序,该应用程序在grails数据库中存储了各种域对象。该应用程序连接到第二个数据库,运行一些原始sql,并在表中显示结果。它基本上是一个报告服务器。 我们通过在DataSo
无法比较来自不同容器的迭代器(参见这里的示例: https://stackoverflow.com/a/4664519/225186 )(或者从技术上讲,它不需要有意义。) 这就提出了另一个问题,来自
我有以下情况: 家长 Activity : ParentActivityClass { private Intent intent; @Override public void onCreate(Bu
我经常将元素与附加功能 Hook ,例如: $('.myfav').autocomplete(); $('.myfav').datepicker(); $('.myfav').click(somefu
因此,我将 tooltipster.js 库用于工具提示,并尝试更改工具提示在不同屏幕尺寸上的默认距离。 所以这是默认的 init 的样子: $(inputTooltipTrigger).tool
我在 ARM7 嵌入式环境中工作。我使用的编译器不支持完整的 C++ 功能。它不支持的一项功能是动态类型转换。 有没有办法实现dynamic_cast<>() ? 我使用 Google 寻找代码,但到
我是一名优秀的程序员,十分优秀!