TeamEnova
asked on
Problems with file upload control
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.
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
Thats a very common error.What is the error code you are getting is it 404 or 500
ASKER
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
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
ASKER
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.
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.
In the internet explorer under options select the option show friendly errors option.
Looks like there is an exception
Looks like there is an exception
ASKER
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.
ASKER
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.
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.
Can you put a try catch block in the upload and catch to see what is the exception
ASKER
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.
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
No,even in your page load i like to see a try catch.Cos there you are doing some database operation right
ASKER
ok. I will put it in around all of the methods to see if I can catch anything. Will post results shortly.
for now just put a global handler onpage load
ASKER
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.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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!
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!