如何爲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

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