javascript 选择上传文件时自动提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/4856916/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Automatically submitting a form when an upload file is chosen
提问by Boris
I have a HTML page with the following input tag:
我有一个带有以下输入标签的 HTML 页面:
...
<input type="file" id="browseContactImageButton" />
... 
Clicking the button on the page results in Open File Dialog. If I want to do the actual uploading, I need another button to click (submit), because this input file button is used just to provide the path to the file.
单击页面上的按钮会打开“打开文件”对话框。如果我想做实际的上传,我需要另一个按钮来点击(提交),因为这个输入文件按钮只是用来提供文件的路径。
Is it possible to click the browse button, select the file and to start the uploading function immediately after the file has been chosen? If yes, could anyone provide the code snippet? Thanks.
是否可以单击浏览按钮,选择文件并在选择文件后立即启动上传功能?如果是,谁能提供代码片段?谢谢。
回答by John Cartwright
If you want the form to submit after the user has made their selection, then simply add
如果您希望在用户做出选择后提交表单,则只需添加
<input type="file" onchange="this.form.submit();" ..... >
回答by Ruwan Kumara
I had the same requirement.
我有同样的要求。
I figured out to use onchange event of fileupload control to fire c# method.
我想出使用文件上传控件的 onchange 事件来触发 c# 方法。
But I'm getting FileUpload1.HasFile property always 'False' and file name was not set yet.
但是我得到 FileUpload1.HasFile 属性总是 'False' 并且文件名尚未设置。
My markup:
我的标记:
<asp:FileUpload ID="FileUpload1" runat="server" ClientIDMode="AutoID" 
                        ViewStateMode="Enabled" onchange="UploadImage();"/>
any suggessions?
有什么建议吗?

