Problem using FileUploader RRS feed

  • Question

  • User1955412872 posted

    I am building a web page that needs to allow users to select a file to upload.

    I have never used the file uploader before, but I don't need to actually move the file anywhere, after they select it I read it and write the info to a database.

    I have the "onchange" set to execute a javascript function that calls a subroutine in my vb code that does the work.

    The problem I am having is that when I select a file and click the "Open button" the file uploader .HasFile (in my VB code) is false UNLESS I put a breakpoint on the line before my IF statement.

    If I do that and then click Continue after selecting the file it works perfectly. If I disable the breakpoint it never selects a file.

    I should point out that when the breakpoint is active, it gets hit multiple times, even though I only selcted the file and hit "Open" once and the FileUploadComplete is only referenced in that one spot.

    I know this sounds crazy!

    Here is what my code looks like:

    ----------------- ASP.net code

    <script type="text/javascript">

    function FileUploadComp() {

    var AttID = '<%=AttachmentID%>'; //document.getElementByID("FileUpload1");
    __doPostBack('FileUpload1', AttID);



    <asp:FileUpload ID="FileUpload1" runat="server" AutoPostBack="true" onchange="FileUploadComp();"/>

    ------------------ VB.net Code

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then

    Else 'If Not IsPostBack

    Dim eventTarget As String
    Dim eventArgument As String

    If (Me.Request("__EVENTTARGET") Is Nothing) Then
    eventTarget = String.Empty
    eventTarget = Me.Request("__EVENTTARGET")
    End If

    If (Me.Request("__EVENTARGUMENT") Is Nothing) Then
    eventArgument = String.Empty
    eventArgument = Me.Request("__EVENTARGUMENT")
    End If

    Dim valuePassed As String = eventArgument


    End If 'If Not IsPostBack
    End Sub

    Sub FileUploadComplete()

    'This is where I put the breakpoint
    debugLine = debugLine + " 100 "

    If FileUpload1.HasFile Then

    ..... this is where the work is done uploading the info from the file

    End Sub

    Thursday, June 13, 2019 6:05 PM

All replies

  • User665608656 posted

    Hi DFoster,

    According to your code, FileUpload control does not have ViewState so that it will not retain the information about the selected file after you postback the page.

    When you use breakpoint, you can read the content of the file, because the page has not been refreshed at this time.

    When you finish this method, the page has been post and refreshed, as a result, the contents of FileUpload are not retained.

    If you just want to get information about the currently selected file, I recommend that you use ajax and webmethod to implement this function.

    For more details, you could refer to the following code:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <script src="../Scripts/jquery-1.10.2.min.js"></script>
        <script type="text/javascript">
            function FileUploadComp(file) { 
                    type: "POST",
                    url: "WebForm_0614_2156609.aspx/FileUploadComplete",
                    data: '{fileName: "' + file.value + '" }',
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (result) {
        <form id="form1" runat="server">
                <asp:FileUpload ID="FileUpload1" runat="server" onchange="FileUploadComp(this);" />

    code behind:

    Imports System
    Imports System.Web.Services
    Namespace WebApplication1.Cases
        Public Partial Class WebForm_0614_2156609
            Inherits System.Web.UI.Page
            Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
                If Not IsPostBack Then
                End If
            End Sub
            Public Shared Sub FileUploadComplete(ByVal fileName As String) 
            End Sub
        End Class
    End Namespace

    In the webmethod FileUploadComplete,you can get the  information about selected file from fileName parameter.

    Best Regards,


    Friday, June 14, 2019 5:42 AM
  • User1955412872 posted

    Thanks for your response. I am trying what you suggested but while testing when I click on the button to select the file I get a login box asking me to login to localhost - how can I get rid of that?

    Friday, June 14, 2019 1:56 PM
  • User1955412872 posted

    OK, so I got rid of the login box, but this method is giving me the same problem that I originally had.

    Friday, June 14, 2019 7:04 PM
  • User665608656 posted

    Hi GDFoster,

    I'm not sure what login box you mentioned, and how did you solve it?

    Is your current code consistent with what I have provided you?

    If not, could you provide your code? This will help us solve your issue more easily.

    According to my last reply, here are the result of that work demo:

    Best Regards,


    Monday, June 17, 2019 6:12 AM