是否可以<input type='file' />使用jQuery 清除控件值?我尝试过以下方法:
$('#control').attr({ value: '' });
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我们有简单的HTML表单<input type="file">,如下所示:
<form>
<label for="attachment">Attachment:</label>
<input type="file" name="attachment" id="attachment">
<input type="submit">
</form>
Run Code Online (Sandbox Code Playgroud)
在IE7(可能是所有着名的浏览器,包括旧的Firefox 2)中,如果我们提交像'// server1/path/to/file/filename'这样的文件,它可以正常工作,并提供文件和文件名的完整路径.
在Firefox 3中,它只返回'filename',因为它们有新的"安全功能"来截断路径,如Firefox错误跟踪系统(https://bugzilla.mozilla.org/show_bug.cgi?id=143220)中所述.
我不知道如何克服这个"新功能",因为它导致我的webapp中的所有上传表单停止在Firefox 3上运行.
任何人都可以帮助找到一个解决方案来获取Firefox 3和IE7上的文件路径?
当我调用val()输入时,type="file"我只获取文件名而不是完整路径.我怎样才能获得完整的路径?
我需要显示当前所选文件的名称(在<input type="file">元素中).
一切都很好,唯一的问题是我得到这种字符串"C:\ fakepath\typog_rules.pdf"(browset自动将其作为输入元素的值).
当我尝试分割字符串'\'或'\\'由于未转义的斜杠而失败时.尝试匹配/替换斜线也失败了.有没有解决的办法?我需要这个至少在Opera和IE中工作(因为在其他浏览器中我可以使用FileReader)
EG我将"C:\ fakepath\typog_rules.pdf"作为输入,并希望得到"typog_rules.pdf"作为输出.
我有以下代码通过HTML5 File API读取文件.我通过input = file element上传了文件.以下是代码块.
<input type="file" id="files" name="file" />
<button id="readFile">Read File</button>
<output id="content"></output>
<script>
function readFile()
{
/* Get the reference of the inpout element. */
var files = document.getElementById('files').files;
console.log(files);
if (!files.length)
{
alert('Please select a file!');
return;
}
/* Reading the first file selected. You can process other files similarly in loop. */
var file = files[0];
/* Instantiate the File Reader object. */
var reader = new FileReader();
/* onLoad event is fired when …Run Code Online (Sandbox Code Playgroud) 我已经尝试使用javascript打开文本文件并获取他的名字和他的内容,所以现在我被困在字符串,因为我使用输入类型文件来获取目录/路径.
无论如何,我的问题是下一个代码有什么问题,我怎样才能使用javascript获取文本文件内容?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Display Text Files</title>
<script type="text/javascript">
var str = document.getElementById('txt').value;
function display() {
if (str != "") {
var filename = str.split("/").pop();
document.getElementById('filename').innerHTML = filename;
}
}
</script>
</head>
<body>
<form method="get" action="#" >
<input type="file" accept="text/plain" id="txt" />
<input type="submit" value="Display Text File" onclick="display();" />
</form>
<a href="#" id="filename"></a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
编辑:我也想在输入文件中禁用所有文件opition(*)到文本文件(.txt).
谢谢!
我有一个公平的R程序,它处理给定目录中的所有文本文件(运行LSA).它有效,但它不完全是用户友好的,我正在尝试使用Shiny来解决这个问题.问题只是给用户一个很好的方法来选择一个目录并获取它的路径; 然后我可以将路径传递给我的R程序.像这样的东西:
fileInput("corpDir", label = "Choose the directory containing the corpus.")
Run Code Online (Sandbox Code Playgroud)
...然后,在Server.R中,通过输入$ corpDir获取路径并将其传递给R程序.但是fileInput小部件做得太多了(我不想上传文件,我只想获取它的路径)而且太少(它返回文件名而不是路径).和R的file.choose会很好 - 好吧,如果它允许选择目录会更好,但让用户在目录中选择一个文件就可以了 - 但是我不知道如何使用file.choose来自一个闪亮的UI.我在RStudio网站上环顾四周,包括他们的示例库,并试图在谷歌小组和这里找到答案.我没有找到这样的东西.任何建议的TIA.