Automatically submitting a form when an upload fil

2019-01-26 06:09发布

问题:

I have a HTML page with the following input tag:

...
<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.

回答1:

If you want the form to submit after the user has made their selection, then simply add

<input type="file" onchange="this.form.submit();" ..... >


回答2:

I had the same requirement.

I figured out to use onchange event of fileupload control to fire c# method.

But I'm getting FileUpload1.HasFile property always 'False' and file name was not set yet.

My markup:

<asp:FileUpload ID="FileUpload1" runat="server" ClientIDMode="AutoID" 
                        ViewStateMode="Enabled" onchange="UploadImage();"/>

any suggessions?