1

I am encountering difficulties when attempting to use the ASP.NET File upload control. What is happening is that when I call the file upload control from the code behind, I always get a FileUploadControl.HasFile = false. I have carried out some research and tried different methods to no avail. The control is being used in the following scenario: The control is contained in a div which is to be called as a popup dialog by means of JQuery. Together with the fileupload control there are two link buttons which will operate the mentioned control (Importing XML from file and manipulate).

Thanks.

Below is a sample of my code.

<asp:scriptmanager id="ScriptManager1" runat="server">
    </asp:scriptmanager>
<div style="text-align: center; margin-left: auto; margin-right: auto">
    <asp:fileupload id="FileUploadControl" runat="server" />
    <asp:updatepanel runat="server">
                <ContentTemplate>                    
                    &nbsp;
                    <asp:LinkButton ID="Append" runat="server" Text="Append" OnClick="Append_Click"></asp:LinkButton>
                    &nbsp;
                    <asp:LinkButton ID="Overwrite" runat="server" Text="Overwrite" OnClick="Overwrite_Click"></asp:LinkButton>
                </ContentTemplate>
                <Triggers>
                    <asp:PostBackTrigger ControlID="Append" />
                    <asp:PostBackTrigger ControlID="Overwrite" />
                </Triggers>
            </asp:updatepanel>
</div>
rick schott
  • 21,012
  • 5
  • 52
  • 81
DottoreM
  • 309
  • 6
  • 27
  • 1
    Afaik file uploads can not be scripted using the AJAX Script Manager stuff. You will have to look out for a different solution. The thing is, Javascript uploads of files work nowadays, but this is not included in the current ASP.NET script managers. You WILL have to do a full postback. This means your page is going to be completely reloaded. – sinni800 Jul 17 '12 at 14:06
  • Are you expecting the file to be uploaded when you click on a button within the `UpdatePanel`? – freefaller Jul 17 '12 at 14:06
  • freefaller, this is the code I put behind. _if (FileUploadControl.HasFile) { try { XDocument document = XDocument.Load(FileUploadControl.FileContent); //code } }_ – DottoreM Jul 17 '12 at 14:09
  • Maybe this could be your problem: http://stackoverflow.com/questions/2264541/fileupload-and-updatepanel-scriptmanager-registerpostbackcontrol-works-the-seco – Josh Jul 17 '12 at 14:11
  • I get it now, the PostBackTrigger is supposed to conduct a full postback instead of an Async one. But how your ASPX looks here, you can just get rid of it completely. Remove both the ScriptPanel and the ScriptManager and the triggers, and we'll see. – sinni800 Jul 17 '12 at 14:11
  • @sinni800, that was the first thing I did... I tried using the control without script panels and managers but it did not work. – DottoreM Jul 17 '12 at 14:12
  • @user1472374 It should work though... It's all about getting the event from the Upload button and checking if the upload control has a file.. Hm – sinni800 Jul 17 '12 at 14:14
  • I have tried just now... this is the resulting code: `
       
    ` FileUploadControl.HasFile still shows as false. @Josh, I will have a look thanks.
    – DottoreM Jul 17 '12 at 14:18

1 Answers1

1

You need to remove the UpdatePanel. You cannot upload a file on partial postback and if all you have in the UpdatePanel triggers the file upload it's not needed:

<div style="text-align: center; margin-left: auto; margin-right: auto">
    <asp:fileupload id="FileUploadControl" runat="server" /> 
        &nbsp;
        <asp:LinkButton ID="Append" runat="server" Text="Append" OnClick="Append_Click"></asp:LinkButton>
        &nbsp;
        <asp:LinkButton ID="Overwrite" runat="server" Text="Overwrite" OnClick="Overwrite_Click"></asp:LinkButton>
</div>

NOTE: General tip for UpdatePanel development. If you run into any weirdness that doesn't make sense remove the UpdatePanel and test your code. UpdatePanel's have lots of limitations depending on your page/controls complexity.

rick schott
  • 21,012
  • 5
  • 52
  • 81
  • I have tried the exact code you have just posted a while ago. It still does not work. Additionally I have inserted the following line of code in the page_load, to no luck. ` Page.Form.Attributes.Add("enctype", "multipart/form-data"); ` – DottoreM Jul 17 '12 at 14:34
  • Thanks, the above was the solution to my problem. The reason why I could not get it to work was because I had some code which was not functioning properly and interfering with the control. (JQuery dialog box). As soon as i fixed that it worked like a charm. – DottoreM Jul 19 '12 at 13:41