如何为HTML输入的文件设置值?

本文翻译自:How to set a value to a file input in HTML?

Note: 注意:

The answers & comments below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element dynamically/programatically using JavaScript and a dataTransfer or FileList object in 2017. 下面的答案和评论反映了2009年旧版浏览器的状态。现在,您实际上可以在2017年使用JavaScript和dataTransfer或FileList对象动态/以编程方式设置文件输入元素的值。

See the answer in this question for details as well as a demo: 有关详细信息和演示,请参见此问题的答案:
How to set file input value programatically (ie: when drag-dropping files)? 如何以编程方式设置文件输入值(即:拖放文件时)?

How can I set the value of this? 如何设置此值?

<input type="file" />

#1楼

参考:https://stackoom.com/question/77Qz/如何为HTML输入的文件设置值


#2楼

You cannot due to security reasons. 由于安全原因,您不能。

Imagine: 想像:

<form name="foo" method="post" enctype="multipart/form-data">
    <input type="file" value="c:/passwords.txt">
</form>
<script>document.foo.submit();</script>

You don't want the websites you visit to be able to do this, do you? 您不希望所访问的网站能够做到这一点,对吗? =) =)


#3楼

You can't. 你不能 And it's a security measure. 这是一种安全措施。 Imagine if someone writes a JS that sets file input value into some sensitive data file? 想象一下,如果有人编写了将文件输入值设置为敏感数据文件的JS?


#4楼

You can't. 你不能

The only way to set the value of a file input is by the user to select a file. 设置文件输入值的唯一方法是用户选择文件。

This is done for security reasons. 出于安全原因这样做。 Otherwise you would be able to create a Javascript that automatically uploads a specific file from the clients computer. 否则,您将能够创建自动从客户端计算机上载特定文件的Javascript。


#5楼

Not an answer to your question (which others have answered), but if you want to have some edit functionality of an uploaded file field, what you probably want to do is: 不是您的问题的答案(其他人都回答了),但是如果您希望对上传的文件字段具有某些编辑功能,则可能需要做的是:

  • show the current value of this field by just printing the filename or URL, a clickable link to download it, or if it's an image: just show it, possibly as thumbnail 通过仅打印文件名或URL,可下载的可下载链接或图像,即可显示该字段的当前值:仅显示它,可能显示为缩略图
  • the <input> tag to upload a new file <input>标记以上传新文件
  • a checkbox that, when checked, deletes the currently uploaded file. 一个复选框,选中该复选框将删除当前上传的文件。 note that there's no way to upload an 'empty' file, so you need something like this to clear out the field's value 请注意,无法上传“空”文件,因此您需要使用类似方法清除字段的值

#6楼

Actually we can do it. 实际上我们可以做到。 we can set the file value default by using webbrowser control in c# using FormToMultipartPostData Library.We have to download and include this Library in our project. 我们可以使用FormToMultipartPostData库在c#中使用webbrowser控件来将文件值设置为默认值。我们必须下载该库并将其包含在我们的项目中。 Webbrowser enables the user to navigate Web pages inside form. Webbrowser使用户可以导航表单内的网页。 Once the web page loaded , the script inside the webBrowser1_DocumentCompleted will be executed. 加载网页后,将执行webBrowser1_DocumentCompleted内部的脚本。 So, 所以,

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
       FormToMultipartPostData postData = 
            new FormToMultipartPostData(webBrowser1, form);
        postData.SetFile("fileField", @"C:\windows\win.ini");
        postData.Submit();
    }

Refer the below link for downloading and complete reference. 请参考以下链接进行下载和完整参考。

https://www.codeproject.com/Articles/28917/Setting-a-file-to-upload-inside-the-WebBrowser-com https://www.codeproject.com/Articles/28917/Setting-a-file-to-upload-inside-the-WebBrowser-com

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章