Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why does the FileUpload always return false for the hasFile status?

Posted on 2006-06-12
3
Medium Priority
?
2,456 Views
Last Modified: 2010-08-05
I am using the FileUpload component but when I try to use it nothing happens.
I debugged and found that the hasFile returns false.

Here is the code I use. (Straight from MSDN)

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FileUpload1 As New FileUpload
        If IsPostBack Then
            Dim path As String = Server.MapPath("~/UploadedImages/")
            Dim fileOK As Boolean = False

            If FileUpload1.HasFile Then
                Dim fileExtension As String
                fileExtension = System.IO.Path. _
                    GetExtension(FileUpload1.FileName).ToLower()
                Dim allowedExtensions As String() = _
                    {".jpg", ".jpeg", ".png", ".gif"}
                For i As Integer = 0 To allowedExtensions.Length - 1
                    If fileExtension = allowedExtensions(i) Then
                        fileOK = True
                    End If
                Next
                If fileOK Then
                    Try
                        FileUpload1.PostedFile.SaveAs(path & _
                             FileUpload1.FileName)
                        Label11.Text = "File uploaded!"
                    Catch ex As Exception
                        Label11.Text = "File could not be uploaded."
                    End Try
                Else
                    Label11.Text = "Cannot accept files of this type."
                End If
            End If
        End If
    End Sub
0
Comment
Question by:mackeyrj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
proten earned 750 total points
ID: 16888142
The first postback that occurs after the fileupload control contains data, is the only time the file uploads.  

If you set the upload control to a file location and then do anything at all to postback the page, the file will be there on the first postback only.  

So, if you are not going to save and/or process the file then, you need to save the file information in a session variable or some location that will persist on multiple postbacks.
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 16891878
so is the file uploading or not or have you answered your own question?
0
 

Author Comment

by:mackeyrj
ID: 16894739
Basicly you are right... I am using view state and the fileupload was in the wrong state so it was being posted before it was being filled with file location
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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 …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 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