FileUpload control clears itself

2019-04-11 11:24发布

问题:

I'm having some trouble with the FileUpload control and was wondering if I could get some help.

On my page I have a FileUpload control, and a drop down list.

So the user browses to the file they want, and then select an option from the drop down (which enables some checkboxes that are also on the page for use, depending on what they select in the drop down). This causes the FileUpload control to become empty, and now the user must browse to the file they wanted again.

Is there anyway to prevent the FileUpload control from losing its contents during the PostBack?

回答1:

Since you tried Relster's suggestion and it didn't work, Spencer is correct. Due to security concerns, it is impossible for anything but the browser to set the path in an <input type="file"/> element. The only solution is to restructure the flow so that the only postback is done when you want to submit the file, and do any other manipulation with client side scripting.

The reason you cannot set the path is because it would allow you to steal users' files. For example, if you hide the input, and have an innocent looking button for postback, you could set the default path of the file input to whatever you wanted to get access to, and the user would upload it without ever knowing what's going on.



回答2:

So, if I understand correctly, the drop down has the autopostback property set to true and you're checking the value of the drop down to make default changes to the check boxes?

Instead try using client side scripting to do the trick.



回答3:

I will most likely be using client side scripting to fix the issue.

However I've found an solution that makes use of ajax for those people who might be interested.

http://forums.asp.net/t/1125781.aspx

From the post by Jessica Cao, which also contains a code example:

"... you could use AJAX to make asynchronous postback to the server, make the partial page which contains the DropDownList to postback, and the page contains the FileUpload control won't postback, so the FileUpload will maintain the FilePath."

This method requires the use of System.Web.Extensions.dll, and as a result the .Net 3.5 Framework.



回答4:

Whenever there's a postback you have to check whether there was also a file coming from the client and save it. You won't be able to re-fill the input box on the client though. I suggest you better hide file upload control (unless you allow multiple file upload) and display filename with check mark beside it. Similar to GMail when you upload files to your email message.

Reasons are security concerns as other pointed out.

I'd also disable any auto postbacks because a drop down change may seem to last an eternity to the client if the upload file size is sufficiently large.



回答5:

You can't do this. A simple Solution is to move to Ajax file Control.



回答6:

I wrapped my autopostback controls inside an asp:updatepanel and that took care of it except for validation failure.



回答7:

The simple solution for preventing file loss from uploadcontrol while postback is to put uploadcontrol outside the updatepanel control on a .aspx page.

eg.

         <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
        </ajax:ToolkitScriptManager>
        <table>
    <tr>
    <td>
<asp:UpdatePanel runat="server" ID="Up_LeaveDetails" UpdateMode="Always">                                 
<ContentTemplate>
<asp:DropDownList ID="DDl_LeaveType" runat="server" CssClass="textfield" Width="150"
                                                                                    AutoPostBack="true" OnSelectedIndexChanged="DDl_LeaveType_SelectedIndexChanged">
                                                                                </asp:DropDownList>
</ContentTemplate>
 </asp:UpdatePanel>
</td>
    <td>
    <asp:FileUpload ID="UploadCertificate" runat="server" />
    </td>
    </tr>
        </table>
    </asp:Content>