Solved

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

Posted on 2006-06-12
3
2,426 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
3 Comments
 
LVL 5

Accepted Solution

by:
proten earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now