I need help with a FileUpload Control in ASP.Net 3.5

HiExperts,
I am using a FileUpload control in my Asp.net 3.5 application with VB.Net codebehind.  I want to set a string variable equal to the FilePath of the file in my FileUploadControl, how do i do that?

Thanks in advance,
mrotor
mainrotorAsked:
Who is Participating?
 
radcaesarConnect With a Mentor Commented:
FileUpload1.PostedFile.FileName="c:\myfile.txt";
0
 
CodeCruiserConnect With a Mentor Commented:
Dim FilePath As string
FilePath = FileUpload1.PostedFile.FileName
0
 
mainrotorAuthor Commented:
CodeCruiser, I tried that and I get an error.  What could be the problem?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
CodeCruiserCommented:
How do I know without knowing what the error is?
0
 
kumarnimavatCommented:
Can you please post the error for us to understand in better way?
0
 
mainrotorAuthor Commented:
Okay below is my code where I want to retrieve the name of the File in the FileUpload control and try to display it in a Label.  If I browse for a file and click my Test button, then the Label accurately displays the message "You have not specified a file."  The minute I do browse for a file I get the generic error Internet Explorer error message.  I have included a Print Screen of the error message.  What is wrong?

    Protected Sub Test_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Test.Click
       
      If (FileUpload1.HasFile = True) Then
            Try
                lblTest.Text = "File name: " & FileUpload1.PostedFile.FileName
            Catch ex As Exception
                lblTest.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else
            lblTest.Text = "You have not specified a file."
        End If
    End Sub

Thanks in advance,
mrotor

Error.bmp
0
 
havj123Connect With a Mentor Commented:
First enable the friendly errors for your web application from IIS and also try to run in Firefox.
0
 
havj123Connect With a Mentor Commented:
Here is the code in VB.Net for File Upload variable string
<%@ Page Language="VB" %>

<script runat="server">
    Protected Sub Button1_Click(ByVal sender As Object, _
      ByVal e As System.EventArgs)
        If FileUpload1.HasFile Then
            Try
                FileUpload1.SaveAs("C:\Uploads\" & _
                   FileUpload1.FileName)
                Label1.Text = "File name: " & _
                   FileUpload1.PostedFile.FileName & "<br>" & _
                   "File Size: " & _
                   FileUpload1.PostedFile.ContentLength & " kb<br>" & _
                   "Content type: " & _
                   FileUpload1.PostedFile.ContentType
            Catch ex As Exception
                Label1.Text = "ERROR: " & ex.Message.ToString()
            End Try
        Else
            Label1.Text = "You have not specified a file."
        End If
    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Upload Files</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" 
         Text="Upload File" />&nbsp;<br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label></div>
    </form>
</body>
</html>

Open in new window

0
 
mainrotorAuthor Commented:
havj123
I stopped getting an error after I added the following code to the web.config:

    <customErrors mode="Off" defaultRedirect="GenericErrorPage.htm">
    </customErrors>

mrotor
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.