Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET FileUpload: how to automatically post back once a file is selected?

Tags:

asp.net

I am working on a ASP.NET app and i have a need to post back to the server after a file is chosen in a FileUpload control without having to have the user explicitly click a 'submit' button. Is this possible? and if so, how?

like image 961
Jason Miesionczek Avatar asked Dec 14 '08 15:12

Jason Miesionczek


2 Answers

I'm assuming you want to make the upload start right away. If so, you should react to the change event in JavaScript, and simply make it submit the form.

<!-- HTML code --->
<input 
  type="file" 
  onchange="if (confirm('Upload ' + this.value + '?')) this.form.submit();"
>

Asking the users for confirmation is recommendable, so they stay in control of the process and can cancel if they chose the wrong file by accident.

like image 62
Tomalak Avatar answered Oct 22 '22 17:10

Tomalak


The first answer had the right javascript, but ASP.NET does not necessarily expose the input control directly, so it is better to put the onchange event on the FileUpload control.

<asp:FileUpload ID="myFileUpload" onchange="if (confirm('Upload ' + this.value + '?')) this.form.submit();" runat="server" />

Another route to go is to provide rich uploading via flash/silverlight/ajax. A great component for this can be found at Ajax Uploader for about $100

like image 29
Abram Simon Avatar answered Oct 22 '22 17:10

Abram Simon