FileUpload Control

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
I have placed a file upload control on my site. I finally for it to work for me but when another user enters my site they either get an error or a message saying it was successful. If they get the success message the file should be on my computer in the proper directory but I go to check and nothing is there. Any ideas? I used pretty much the exact reference from MS.com. Here is the link for the reference I used. I cut and paste and only changed the directory and fileExtension types of course.
http://msdn2.microsoft.com/en-us/library/ms227669.aspx
 
VB.NET:
 Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim path As String = "C:\Music\My Music\Online_Users\"
        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() = {".mp3"}
            For i As Integer = 0 To allowedExtensions.Length - 1
                If fileExtension = allowedExtensions(i) Then
                    fileOK = True
                End If
            Next
            If fileOK Then
                Try
                    FileUpload1.SaveAs(path & FileUpload1.FileName)
                    Me.LabelUploadInfo.Text = "File uploaded!"
                Catch ex As Exception
                    Me.LabelUploadInfo.Text = ex.Message
                End Try
            Else
                Me.LabelUploadInfo.Text = "Cannot accept files of this type."
            End If
            Return
        End If
    End Sub

I know i don't need a loop for the file extensions and all but I just copied from MS.com and I am reserving that same code for later use.
 
I have modified the code to look like so after reading a book and hopefully this helps me with som eproblems but I don't think it was the problem. I tried the postedfile properties once before also and got the same problem.

VB.NET:
 Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Dir As String = "C:\Music\My Music\Online_Users\"
        Dim fileOK As Boolean = False
        If FileUpload1.HasFile Then
            Dim fileExtension As String
            fileExtension = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToLower()
            Dim allowedExtensions As String() = {".mp3"}
            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.Combine(Dir, Path.GetFileName(FileUpload1.PostedFile.FileName)))
                    Me.LabelUploadInfo.Text = "File uploaded!"
                Catch ex As Exception
                    Me.LabelUploadInfo.Text = ex.Message
                End Try
            Else
                Me.LabelUploadInfo.Text = "Cannot accept files of this type."
            End If
            Return
        End If
    End Sub
 
Ok, I found the first problem. I had to update the upload file size limit with my web.config file. So it worked for a short time. Then I moved my directory to become my default directory. I don't know what settings or whatever changed but the site application is exactly the same. Now i get the error message that 'C:\'the directory I am saving to' access is denied.' So not myself or users can upload anything. How come the directory is denied? It is a local path on my computer and the same directory I was using before to save the files and it worked.
 
Thanks JohnH. I have read the MS article once before but I didn't have a Security's tab so I thought I was at the wrong place. So I took my time the first link described my problem exactly. Basically I had to google a little more but I found out that in XP if your not part of a domain by default you don't have a security's tab on your folders. So in the folder click Tools/Folder Options then the View Tab. Then I scrolled all the way down and un-clicked "Use Simple File Sharing". I read that from somewhere and after words I had a security tab. From there I used the info in the articles you sent to configure my ASPNET account to access the files and it works. Thanks :)
 

Latest posts

Back
Top