File not uploading but not getting errors.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dougancil
    Contributor
    • Apr 2010
    • 347

    File not uploading but not getting errors.

    I have a site that I've almost completed and while testing it today noticed that it's not uploading any files or doing the bulk insert in SQL that I need it to. Here is my codebehind:

    Code:
    Imports System.IO
    Imports System.Data   
    Imports System.Data.SqlClient
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Submit1_Click(ByVal sender As Object, ByVal e As EventArgs)
    
            If Not File1.PostedFile Is Nothing And File1.PostedFile.ContentLength > 0 Then
                Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)
                Dim SaveLocation As String = Server.MapPath("Data") & "\" & fn
                Try
                    File1.PostedFile.SaveAs(SaveLocation)
                    Response.Write(<center>Thank you for your submission.</center>)
    
                    ' get the file that was submitted and check if it was .txt file   
                    Dim theFile As FileInfo = New FileInfo(SaveLocation)
                    If theFile.Extension <> ".txt" Then
                        Response.Write(<center>Please submit a text file.</center>)
                    End If
    
                    Dim importPath As String = Server.MapPath("Data") & "\upload.txt"
                    If File.Exists(importPath) Then
                        ' do something with existing upload.txt file, maybe archive?   
                    End If
    
                    ' rename the uploaded file to upload.txt for importing   
                    theFile.MoveTo(importPath)
    
                    ' and bulk import the data:   
                    Dim connection As String = ConfigurationManager.ConnectionStrings("Dialerresults").ConnectionString
                    Dim results As New DataTable
    
                    Using con As New SqlConnection(connection)
                        con.Open()
    
                        ' execute the bulk import   
                        Using cmd As SqlCommand = con.CreateCommand
                            cmd.CommandText = "bulk insert dialerresults from '" & importPath & "' " & _
                                " with ( fieldterminator = ',', rowterminator = '\n' )"
                            cmd.ExecuteNonQuery()
                        End Using
                    End Using
    
                Catch Exc As Exception
                    Response.Write("Error: " & Exc.Message)
                End Try
            Else
                Response.Write(<center>Please select a file to upload.</center>)
            End If
    
        End Sub
    End Class
    Can anyone see why the file is not being uploaded? I'm sure its something simple but I've been working on this for a while and may have missed something. Any assistance would be welcome.

    Thank you,

    Doug
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    First thing that comes to mind is that you've placed your File1 in an UpdatePanel.
    UpdatePanels do not support the upload of files...the files will simply not get to the server.

    -Frinny

    Comment

    • dougancil
      Contributor
      • Apr 2010
      • 347

      #3
      Frinny,

      I'm sorry but I'm new to asp.net and know nothing of UpdatePanels. What would be a good workaround for this issue?

      Thank you,

      Doug

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Well if you don't know what an UpdatePanel is then it's very unlikely that you are using it. So just ignore my previous point.

        To be quite honest, after skimming through your code right now, I'm not sure that your posted code would even compile. Line 18 is going to cause you problems.


        Have you tried stepping through the application and watching what happens to see if you can figure out what's going on?

        -Frinny

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          You should probably check out this MSDN documentation on the FileUpload control. It has a working example that you could probably use to get you started.

          -Frinny

          Comment

          • dougancil
            Contributor
            • Apr 2010
            • 347

            #6
            Frinny,

            This code is comprised of code I've written and something that someone else wrote. The code does compile and I have tried loading two different files (one a .txt file and one a .pdf file) and neither would show up in the destination folder. I've built the page, and debugged it with no errors. When I tried to upload the .pdf page, what should have happened, was that I should have received an error that that files of that format were not accepted and I didn't get that message either. Any ideas as to what may be causing that? Would those be symptoms of a UpdatePanel issue?

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              If you had put your FileUpload control into an UpdatePanel then the FileUpload control's PostedFile will always be nothing.

              If you have done this, take the FileUpload control out of the UpdatePanel.

              Comment

              • dougancil
                Contributor
                • Apr 2010
                • 347

                #8
                Frinny,

                Can you point out to me where the UpdatePanel is in my code? I'll gladly take the lines of code out once I can identify them.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  You would have to post the ASPX page "code" (look at the page and click "source" view button).

                  I can't tell unless you post that code.

                  Comment

                  • dougancil
                    Contributor
                    • Apr 2010
                    • 347

                    #10
                    Code:
                    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
                    
                    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                    
                    <html xmlns="http://www.w3.org/1999/xhtml">
                    <head runat="server">
                        <title>Upload Page</title>
                        <style type="text/css">
                            #form1
                            {
                                height: 128px;
                            }
                            #File1
                            {
                                top: 66px;
                                }
                            #Submit1
                            {
                                top: 103px;
                                left: 591px;
                            }
                            #TextArea1
                            {
                                z-index: 1;
                                left: 303px;
                                top: 45px;
                                position: absolute;
                            }
                        </style>
                    <script language="javascript" type="text/javascript">
                    // <!CDATA[
                    
                    function Submit1_onclick() {
                    
                    }
                    
                    // ]]>
                    </script>
                    </head>
                    <body>
                        <form id="form1" enctype="multipart/form-data" runat="server">
                        <br />
                       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <br />
                    <input type="File" id="File1" name="File1" runat="server" 
                            style="position: absolute; left: 512px;" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <br />
                    <input type="Submit" id="Submit1" value="Upload" runat="server" 
                            style="position: absolute; height: 30px;" 
                            onclick="return Submit1_onclick()" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        </form>
                    </body>
                    </html>
                    Frinny, there is my code for the page. As you can see, there's not much too it. I know that it's not really clean but I'll worry about cleaning it up later. Right now I'm just trying to get the "mechanics" of this working.

                    Thank you for taking a look at it,

                    Doug

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Oh....
                      Well you aren't using an UpdatePanel ;)
                      But you are using an HTML input element.

                      You should be using the ASP.NET FileUpload control control instead!!

                      Comment

                      • Frinavale
                        Recognized Expert Expert
                        • Oct 2006
                        • 9749

                        #12
                        Oh wait there's another thing
                        You may not even be reaching the code that you have posted because you don't have an asp.net Button on the page either!

                        Change your submit HTML button into an ASP.NET button as well.

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          Like this:
                          Code:
                          <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
                           
                          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
                           
                          <html xmlns="http://www.w3.org/1999/xhtml">
                          <head runat="server">
                              <title>Upload Page</title>
                              <style type="text/css">
                                  #form1
                                  {
                                      height: 128px;
                                  }
                                  #File1
                                  {
                                      top: 66px;
                                      }
                                  #Submit1
                                  {
                                      top: 103px;
                                      left: 591px;
                                  }
                                  #TextArea1
                                  {
                                      z-index: 1;
                                      left: 303px;
                                      top: 45px;
                                      position: absolute;
                                  }
                              </style>
                          
                          </head>
                          <body>
                              <form id="form1" enctype="multipart/form-data" runat="server">
                              <br />
                             
                              <br />
                          <asp:FileUpload ID="File1" runat="server" 
                                  style="position: absolute; left: 512px;" />
                          <br />
                          <asp:Button id="Submit1"  runat="server" 
                                  style="position: absolute; height: 30px;" 
                                  onclick="Submit1_Click" />
                          </body>
                          </html>

                          Comment

                          • dougancil
                            Contributor
                            • Apr 2010
                            • 347

                            #14
                            Frinny,

                            So if I change the html button to an asp.net button will it automatically rewrite the codebehind to reflect the upload controls in Visual Studio?

                            Comment

                            • Frinavale
                              Recognized Expert Expert
                              • Oct 2006
                              • 9749

                              #15
                              I'm not sure.

                              The C# IDE is different the the VB.NET IDE and I work with VB.NET most of the time.

                              But typically what happens is that when you change your HTML code into an ASP.NET control, that control is "registered " into the .designer.cs file so that it is accessable to work in both the ASPX code and your C# code.

                              ASP.NET controls have 2 aspects to them: client side and server side. In fact when your application has finished executing the server-side code it Renders the page's output which is sent to the browser. The only thing the browser understands is HTML (as you already know). So when the server-side code finishes executing it turns all of the ASP.NET controls into HTML.

                              The important thing is that the HTML is generated and maintained by ASP.NET

                              This means that if the user clicks an ASP.NET button on the page, you can handle the buttons Click Event in your C# server code.

                              If you use just regular HTML for the button then there is no Click Event generated because ASP.NET isn't in control of it.

                              So it's important that you use ASP.NET controls when developing ASP.NET applications/websites :)


                              Anyways,

                              You declare your button in the ASPX code as I have shown you...This will give you access events and properties (and a lot more) for the button in your C# code that you never had before.

                              Comment

                              Working...