ViewState support for FileUpload control
I am creating FileUpload
controls at runtime. When I click the button LinkButton
a new control is created FileUpload
.
Now suppose I have selected a file from the control FileUpload
and I click a button LinkButton
. The previous control FileUpload
loses its path. However, I maintain the ViewState of every control I create at runtime using this line:
f1.enableviewstate = true;
How do I save the selected file for the control FileUpload
?
a source to share
Steps
- user selects a file
- the user clicks the LinkButton (throws a postback that adds additional control over file uploads)
- the server side should receive the file on postback and save it somewhere (anywhere)
- replace the first one
<input type=file>
with something like a label and a checkmark icon (to inform the user that the file has already been uploaded (or even a read-only textbox with the browse button disabled to fake file upload control), however you won't be able to display the correct path in it to the file) - the user is presented with a new form in which there is a new empty file upload control when showing already uploaded files.
For security reasons, you cannot manipulate <input type=file>
the shape or form in any way.
A Hacked Approach
If you understand correctly, your link button will add additional file upload controls to your page. Instead, I created enough load controls the first time and only displayed one. Others will be hidden by CSS. After the user clicks the LinkButton, it will only have client side Javascript functionality which will expose additional controls. And more ... and more ... and more ... until the maximum is reached.
An integrated approach
However, you can do it differently, using more Javascript and make it more Web 2.0-ish. However, you must download these files via<iframe>
a source to share
like some of the ones mentioned above, you cannot save the FileUpload viewstate due to security issues. What you can do is just add a shortcut just below FileUpload. When the user clicks on the link to create a new FileUpload, a callback will be sent where you can check if the FileUpload controls on the page have any value (that is, the user has already selected the file to upload), and if so, you can immediately start downloading this file and show the result (path or filename) on the shortcut, only the user knows that he has already added this file. You can also hide the file download and optionally add a delete link to delete the downloaded file again (similar approach as Gmail).
Hope this helped.
Juri
a source to share