[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-04-25
9
Medium Priority
?
371 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:mainrotor
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 9

Accepted Solution

by:
radcaesar earned 500 total points
ID: 35462565
FileUpload1.PostedFile.FileName="c:\myfile.txt";
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 500 total points
ID: 35462660
Dim FilePath As string
FilePath = FileUpload1.PostedFile.FileName
0
 

Author Comment

by:mainrotor
ID: 35462782
CodeCruiser, I tried that and I get an error.  What could be the problem?
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 83

Expert Comment

by:CodeCruiser
ID: 35462833
How do I know without knowing what the error is?
0
 
LVL 3

Expert Comment

by:kumarnimavat
ID: 35462842
Can you please post the error for us to understand in better way?
0
 

Author Comment

by:mainrotor
ID: 35463700
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
 
LVL 6

Assisted Solution

by:havj123
havj123 earned 1000 total points
ID: 35465872
First enable the friendly errors for your web application from IIS and also try to run in Firefox.
0
 
LVL 6

Assisted Solution

by:havj123
havj123 earned 1000 total points
ID: 35465887
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
 

Author Comment

by:mainrotor
ID: 35468561
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview
Suggested Courses

830 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