Solved

Problems with file upload control

Posted on 2009-04-10
14
1,293 Views
Last Modified: 2012-05-06
I am struggling to get this working and it should be pretty straight forward.

I am using a masterpage with a form tag on it.  The content placeholders are inside the form tag.

When I run the page, I get the following error:
"Internet Explorer cannot display the web page."

What am I doing wrong?

BTW, the button.click event is not complete, as I have to do the sql part, but it should still upload the file.


*****************************ASPX**************************
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false"
    CodeFile="uploadpics.aspx.vb" Inherits="uploadpics" Title="Untitled Page" %>
 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
    <div id="divInstructions" style="font-size: small">
        <h3>
            How To Upload Pictures</h3>
        <p>
            Uploading files is pretty simple, once you get the hang of it. Just follow these
            simple steps to get your picture online and in a photo album!</p>
        <ol>
            <li>Click the browse button. You will see a box pop up that shows your computer. Browse
                out to the filel, select it, and click OK.</li><li>Now you can select a Photo Album
                    from the drop down list. Just select the album that makes the most sense to you.
                    You will see some pre-created albums in the list. If one of those suits your needs,
                    then you can use it.</li><li>In the description box, type in your description of the
                        picture. It should be short and concise.</li>
            <li>Now select the keywords that are associated with the file. One list has the names
                of the players in the picture and the other one has a list of games you can choose
                from. For multiple players, hold down the control key ('ctrl') and click the players
                you want. </li>
            <li>You can also type in your own keywords in the box below the lists. Type in the keyword
                and click the green check mark next to the box. We use the key words to search on
                when someone wants to create a custom slide show!</li>
            <li>Once you have that all done, click the green check mark and we take over from there.
                If you decide to cancel your upload and want to clear out all of the information
                you have selected, click the red cancel button.</li></ol>
    </div>
    <asp:FileUpload ID="FileUpload1" runat="server" Width="100%" />
    <table>
        <tr>
            <td>
                Album:
            </td>
            <td colspan="2">
                <asp:DropDownList ID="ddlAlbums" runat="server">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td valign="top">
                Description:
            </td>
            <td colspan="2">
                <asp:TextBox ID="tbxDescription" runat="server" Width="400px"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td style="text-align: center">
                Players
            </td>
            <td style="text-align: center">
                Games
            </td>
        </tr>
        <tr>
            <td valign="top">
                Keywords:
            </td>
            <td>
                <asp:ListBox ID="lbxPlayers" runat="server" Height="200px" Width="200px" SelectionMode="Multiple">
                </asp:ListBox>
            </td>
            <td>
                <asp:ListBox ID="lbxGames" runat="server" Height="200px" Width="200px" SelectionMode="Multiple">
                </asp:ListBox>
            </td>
        </tr>
        <tr>
            <td>
                Custom Keywords:
            </td>
            <td colspan="2">
                <asp:TextBox ID="tbxCustomKW" runat="server" Width="400px"></asp:TextBox>
                <asp:ImageButton ID="btnAddCustomKW" runat="server" ImageUrl="~/images/icons/ok_16.gif" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td colspan="2">
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Label ID="lblCustomKW" runat="server" ForeColor="Silver"></asp:Label>
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="btnAddCustomKW" />
                    </Triggers>
                </asp:UpdatePanel>
            </td>
        </tr>
        <tr>
            <td valign="top">
            </td>
            <td>
            </td>
            <td style="text-align: right">
                <asp:ImageButton ID="btnUpload" runat="server" ImageUrl="~/images/icons/ok_32.gif" />
                <asp:ImageButton ID="btnCancel" runat="server" ImageUrl="~/images/icons/cancel_32.gif" />
            </td>
        </tr>
    </table>
</asp:Content>
 
********************Content Page Code Behind**********************
Imports System.IO
Partial Class uploadpics
    Inherits System.Web.UI.Page
    Dim dbF As New dbfunctions
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim dsAlbums, dsPlayers, dsGames As Data.DataSet
        dsAlbums = dbF.ReturnDataSetFromStoredProcedure("usp_GetAlbums")
        dsPlayers = dbF.ReturnDataSetFromStoredProcedure("usp_GetPlayerNames")
        dsGames = dbF.ReturnDataSetFromStoredProcedure("usp_GetAllMatches")
 
        With ddlAlbums
            .DataSource = dsAlbums
            .DataTextField = "albumname"
            .DataValueField = "id"
            .DataBind()
        End With
        With lbxPlayers
            .DataTextField = "Name"
            .DataSource = dsPlayers
            .DataBind()
        End With
        With lbxGames
            .DataTextField = "opponent"
            .DataSource = dsGames
            .DataBind()
        End With
 
    End Sub
 
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnUpload.Click
 
        'Collect the data into variables
        Dim sFileName, sFilePath, sFullPath, sDescription, sKeywords As String
 
        sFileName = CreateFilename(fileUpload1.PostedFile.ToString)
        sFilePath = Server.MapPath(".") & "/pics/"
        sFullPath = sFilePath & sFileName
        sDescription = tbxDescription.Text
        sKeywords = ""
        For Each i As ListItem In lbxPlayers.Items
            sKeywords = sKeywords & i.Text & ";"
        Next
        sKeywords = sKeywords & lblCustomKW.Text
        Dim bApproved As Boolean = False
        Dim sUser As String = Profile.UserName
        Dim dUploadTime As Date = Now()
 
        If Not fileUpload1.PostedFile Is Nothing And fileUpload1.PostedFile.ContentLength > 0 Then
 
            Dim fn As String = System.IO.Path.GetFileName(fileUpload1.PostedFile.FileName)
            Try
                fileUpload1.PostedFile.SaveAs(sFullPath)
                Response.Write("The file has been uploaded.")
            Catch Exc As Exception
                Response.Write("Error: " & Exc.Message)
            End Try
        Else
            Response.Write("Please select a file to upload.")
        End If
 
    End Sub
 
    Protected Function CreateFilename(ByVal s As String) As String
 
        Dim sFileType As String = s.Substring(s.LastIndexOf("."))
        Return Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & sFileType
 
    End Function
 
    Protected Sub btnAddCustomKW_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAddCustomKW.Click
        Dim sNewKW, sOldKW As String
        sNewKW = tbxCustomKW.Text
        sOldKW = lblCustomKW.Text
        If lblCustomKW.Text.Length < 1 Then
            lblCustomKW.Text = sNewKW
        Else
            lblCustomKW.Text = sOldKW & ";" & sNewKW
        End If
        tbxCustomKW.Text = Nothing
    End Sub
End Class

Open in new window

0
Comment
Question by:TeamEnova
  • 8
  • 6
14 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 24121009
Thats a very common error.What is the error code you are getting is it 404 or 500
0
 
LVL 2

Author Comment

by:TeamEnova
ID: 24122232
Its not giving me an error.  Its weird.  I have never seen this before.

I am working on a dev machine and using the locally installed asp server.

I have attached a screen shot of the page the pops up after I click the submit button.
cl-errorpage.gif
0
 
LVL 2

Author Comment

by:TeamEnova
ID: 24122243
Another oddity.....

This morning, I opened up VS2008, opened up this dev site, and ran it.  I had inserted a breakpoint in a location where I wanted to watch the goings on.

It went to the breakpoint (first time that has happened).  I had a problem with a variable, so I stopped the debug session, fixed the error, and when I tried to run it again, it went back to the problem above.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:spprivate
ID: 24122265
In the internet explorer under options select the option show friendly errors option.
Looks like there is an exception
0
 
LVL 2

Author Comment

by:TeamEnova
ID: 24122305
I already have that set.  So I didnt change anything.  But I went back in to run it to see if the options were different when I launched it in debug and they were not.  But, it took me to the breakpoint this time.
0
 
LVL 2

Author Comment

by:TeamEnova
ID: 24122318
LOL, this is getting really weird.

I ran it again, just to see what would happen.  It worked flawlessly.  It took me back to the uploadpics.aspx page, as designed.  I selected another file, clicked the button, and got the error.
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24122325
Can you put a try catch block in the upload and catch to see what is the exception
0
 
LVL 2

Author Comment

by:TeamEnova
ID: 24122347
Its already there.  If I put a breakpoint on that method, it never reaches the breakpoint if I get the weird page.

Attached is the updated code as I have continued to work on it.
Imports System.IO
Partial Class uploadpics
    Inherits System.Web.UI.Page
    Dim dbF As New dbfunctions
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        Dim dsAlbums, dsPlayers, dsGames As Data.DataSet
        dsAlbums = dbF.ReturnDataSetFromStoredProcedure("usp_GetAlbums")
        dsPlayers = dbF.ReturnDataSetFromStoredProcedure("usp_GetPlayerNames")
        dsGames = dbF.ReturnDataSetFromStoredProcedure("usp_GetAllMatches")
 
        With ddlAlbums
            .DataSource = dsAlbums
            .DataTextField = "albumname"
            .DataValueField = "id"
            .DataBind()
        End With
        With lbxPlayers
            .DataTextField = "Name"
            .DataSource = dsPlayers
            .DataBind()
        End With
        With lbxGames
            .DataTextField = "opponent"
            .DataSource = dsGames
            .DataBind()
        End With
 
    End Sub
 
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnUpload.Click
 
        If Not FileUpload1.PostedFile Is Nothing And FileUpload1.PostedFile.ContentLength > 0 Then
 
            'Collect the data into variables
            Dim sFileName, sFilePath, sFullPath, sDescription, sKeywords As String
 
            sFileName = CreateFilename(FileUpload1.PostedFile.FileName.ToString)
            sFilePath = Server.MapPath(".") & "/pics/"
            sFullPath = sFilePath & sFileName
            sDescription = tbxDescription.Text
            sKeywords = ""
            For Each i As ListItem In lbxPlayers.Items
                If i.Selected = True Then
                    sKeywords = sKeywords & i.Text & ";"
                End If
            Next
            sKeywords = sKeywords & lblCustomKW.Text
            Dim bApproved As Boolean = False
            Dim sUser As String = Profile.UserName
            Dim dUploadTime As Date = Now()
 
            Try
                FileUpload1.PostedFile.SaveAs(sFullPath)
                ClearFileUploadFields()
                lblResult.Text = "The file was successfully uploaded."
            Catch Exc As Exception
                Response.Write("Error: " & Exc.Message)
            End Try
        Else
            Response.Write("Please select a file to upload.")
        End If
 
    End Sub
 
    Protected Sub ClearFileUploadFields()
        tbxDescription.Text = ""
        lbxGames.SelectedIndex = Nothing
        lbxPlayers.SelectedIndex = Nothing
        tbxCustomKW.Text = ""
        lblCustomKW.Text = ""
 
    End Sub
    Protected Function CreateFilename(ByVal s As String) As String
 
        Dim sFileType As String = s.Substring(s.LastIndexOf("."))
        Return Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & sFileType
 
    End Function
 
    Protected Sub btnAddCustomKW_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAddCustomKW.Click
        Dim sNewKW, sOldKW As String
        sNewKW = tbxCustomKW.Text
        sOldKW = lblCustomKW.Text
        If lblCustomKW.Text.Length < 1 Then
            lblCustomKW.Text = sNewKW
        Else
            lblCustomKW.Text = sOldKW & ";" & sNewKW
        End If
        tbxCustomKW.Text = Nothing
    End Sub
End Class

Open in new window

0
 
LVL 15

Expert Comment

by:spprivate
ID: 24122355
No,even in your page load i like to see a try catch.Cos there you are doing some database operation right
0
 
LVL 2

Author Comment

by:TeamEnova
ID: 24122360
ok.  I will put it in around all of the methods to see if I can catch anything.  Will post results shortly.
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24122399
for now just put a global handler onpage load
0
 
LVL 2

Author Comment

by:TeamEnova
ID: 24122415
Test 1
     1.  Page loaded fine.  No errors
     2.  Populated all fields, no errors
     3.  Clicked submit button, all went well and file was uploaded.
Test 2
     1.  After first upload, uploadpics.aspx was returned as exptected, no errors
     2.  Populated all fields, no errors
     3.  Clicked submit button, all went well and file was uploaded.
Test 3
     1.  After first upload, uploadpics.aspx was returned as exptected, no errors.  This time, I stopped debugging and restarted it.
     2.  Populated all fields, no errors
     3.  Clicked submit button, it did not return any errors, except that it took me to that weird page.
Imports System.IO
Partial Class uploadpics
    Inherits System.Web.UI.Page
    Dim dbF As New dbfunctions
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim dsAlbums, dsPlayers, dsGames As Data.DataSet
            dsAlbums = dbF.ReturnDataSetFromStoredProcedure("usp_GetAlbums")
            dsPlayers = dbF.ReturnDataSetFromStoredProcedure("usp_GetPlayerNames")
            dsGames = dbF.ReturnDataSetFromStoredProcedure("usp_GetAllMatches")
 
            With ddlAlbums
                .DataSource = dsAlbums
                .DataTextField = "albumname"
                .DataValueField = "id"
                .DataBind()
            End With
            With lbxPlayers
                .DataTextField = "Name"
                .DataSource = dsPlayers
                .DataBind()
            End With
            With lbxGames
                .DataTextField = "opponent"
                .DataSource = dsGames
                .DataBind()
            End With
 
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
 
    End Sub
 
    Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnUpload.Click
        Try
            If Not FileUpload1.PostedFile Is Nothing And FileUpload1.PostedFile.ContentLength > 0 Then
 
                'Collect the data into variables
                Dim sFileName, sFilePath, sFullPath, sDescription, sKeywords As String
 
                sFileName = CreateFilename(FileUpload1.PostedFile.FileName.ToString)
                sFilePath = Server.MapPath(".") & "/pics/"
                sFullPath = sFilePath & sFileName
                sDescription = tbxDescription.Text
                sKeywords = ""
                For Each i As ListItem In lbxPlayers.Items
                    If i.Selected = True Then
                        sKeywords = sKeywords & i.Text & ";"
                    End If
                Next
                sKeywords = sKeywords & lblCustomKW.Text
                Dim bApproved As Boolean = False
                Dim sUser As String = Profile.UserName
                Dim dUploadTime As Date = Now()
 
                FileUpload1.PostedFile.SaveAs(sFullPath)
                ClearFileUploadFields()
                lblResult.Text = "The file was successfully uploaded."
            Else
                lblResult.Text = "Please select a file to upload."
            End If
        Catch Exc As Exception
            lblResult.Text = "Error: " & Exc.Message
        End Try
 
    End Sub
 
    Protected Sub ClearFileUploadFields()
        Try
            tbxDescription.Text = ""
            lbxGames.SelectedIndex = Nothing
            lbxPlayers.SelectedIndex = Nothing
            tbxCustomKW.Text = ""
            lblCustomKW.Text = ""
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
 
    End Sub
    Protected Function CreateFilename(ByVal s As String) As String
 
        Dim sFileType As String = s.Substring(s.LastIndexOf("."))
        Return Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & Now.Hour.ToString & Now.Minute.ToString & Now.Second.ToString & sFileType
 
    End Function
 
    Protected Sub btnAddCustomKW_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnAddCustomKW.Click
        Try
            Dim sNewKW, sOldKW As String
            sNewKW = tbxCustomKW.Text
            sOldKW = lblCustomKW.Text
            If lblCustomKW.Text.Length < 1 Then
                lblCustomKW.Text = sNewKW
            Else
                lblCustomKW.Text = sOldKW & ";" & sNewKW
            End If
            tbxCustomKW.Text = ""
        Catch ex As Exception
            Response.Write(ex.ToString)
        End Try
    End Sub
End Class

Open in new window

0
 
LVL 15

Accepted Solution

by:
spprivate earned 500 total points
ID: 24122492
Ok here is some interesting thing.This is an odd behaviour of file upload control in asp,net cos asp.net tries to upload file in one shot  comparing to other uploaders.So there are couple of config settings like file size.

http://virendrablogs.blogspot.com/2007/11/aspnet-20-file-upload-control.html

Also it talks about the position of the control

Check these out
0
 
LVL 2

Author Closing Comment

by:TeamEnova
ID: 31569139
Wow.

After makeing the changes suggested in the article, I ran it about 20 times, chosing files with a filesize of over 5Meg.  Everything works great!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question