• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 343
  • Last Modified:

Upload File with Ajax does not work the first time it is initiated

I have an ASP.Net VB application with AJAX panels that has a bizarre behavior:

Client Side Code and Server Side code below.

The button is set to trigger a full postback (and does do that - even on the first time when it doesn't upload the file)

On the first run - server error returned on line ("imgContentType = MyFile.PostedFile.ContentType") is:
System.NullReferenceException: Object reference not set to an instance of an object.

All future times - works perfect...until you go to a different page and return.
<input id="MyFile" type="file" runat="Server" size="40" />
<input id="btnPictureUpload" type="submit" value="Upload" onserverclick="Upload_Click" runat="Server" />                                                                    <div id="UploadDetails" visible="False" runat="Server">
File Name: <span id="FileName" runat="Server" />
<br>
File Content: <span id="FileContent" runat="Server" />
<br>
File Size: <span id="FileSize" runat="Server" />bytes
<br>
</div>
 
SERVER SIDE
Dim imgContentType As String
imgContentType = MyFile.PostedFile.ContentType

Open in new window

0
PapaLorax
Asked:
PapaLorax
  • 2
1 Solution
 
ArlowinCommented:
I've used a slightly different solution.  This code runs in an update panel within a tab panel and works fine.

 <asp:UpdatePanel ID="upUpload" runat="server">
        <Triggers>
            <asp:PostBackTrigger ControlID="btnUpload" />
        </Triggers>
        <ContentTemplate>
        <div class="TabPanel">
            <asp:Panel ID="pnlUpload" runat="server">
            <br />
            <asp:Label ID="Label2" runat="server" Text="Select an attachment to save. Document size is limited to 4MB."
                Width="492px"></asp:Label>
            <br />
            <asp:FileUpload ID="fupAttachments" runat="server" Width="682px"  />
            <br />
            <br />
            <asp:Label ID="Label1" runat="server" Text="Description (Maximum 1000 characters.)"
                Width="267px"></asp:Label>
            <br />
            <asp:TextBox ID="txtDesc" runat="server" Height="44px" MaxLength="1000" Width="674px"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="btnUpload" runat="server" Text="Upload" ToolTip="Click to save an attachment" />
            <br />
            <br />
            Additonal Instructions:<br />
            <br />
            Click the 'Browse' button. Select the file to upload, then click the 'Upload' button.<br />
            </asp:Panel>
        </div>
        </ContentTemplate>
        </asp:UpdatePanel>
 
Code Behind:
Public Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpload.Click
        If (fupAttachments.HasFile) Then
            Try
                If (SaveFile(fupAttachments)) Then
                    ShowMessage("File Uploaded: " & fupAttachments.FileName & "; File Size: " & fupAttachments.PostedFile.ContentLength _
                        & "; File Type: " & fupAttachments.PostedFile.ContentType, False)
                Else
                    ShowMessage("The file could not be saved. Please report this to the system administrator.", True)
                End If
            Catch ex As Exception
                ShowMessage("The following error has occured: " & ex.ToString, True)
            End Try
        Else
            ShowMessage("You must specify a file to save.", True)
        End If
    End Sub

Open in new window

0
 
PapaLoraxAuthor Commented:
I am so frustrated - I can get your application to work. I can actually get a dummy setup to work for me too...but when I go back to my app, first time = "No file" all subsequent times...perfect.
0
 
PapaLoraxAuthor Commented:
It was because the Input Box was Visible=False at page load.

Once changed it was fine.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now