views:

331

answers:

5

When I try to post a file its coming back false ie there was no file attached. Can anyone see anything wrong with this? Or what might be causing it.

<form id="Form1" enctype="multipart/form-data" method="post" runat="server">
    <asp:FileUpload ID="fileUpload" runat="server" />
    <asp:Button ID="cmdSubmitApplication" runat="server" Text="Button" />
</form>

Protected Sub cmdSubmitApplication_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmitApplication.Click

    If Me.fileUpload.PostedFile Is Nothing Then
        Response.Write("You must specify file to upload!")
    Else

        Try

            Dim strExt As String = Path.GetExtension(Me.fileUpload.PostedFile.FileName)

            If strExt.ToLower() = ".doc" Then

                Dim savedFile As String
                savedFile = Path.GetFileName(Me.fileUpload.PostedFile.FileName)
                Me.fileUpload.PostedFile.SaveAs(Server.MapPath("cvs\") & savedFile)
                Response.Write("File Uploaded Successfully")

            Else
                Response.Write("Only Image Files are Allowed")
            End If

        Catch exp As Exception
            Response.Write(exp.Message)
        End Try

    End If

End Sub
A: 

here is a full working example from MSDN:

http://msdn.microsoft.com/en-us/kb/kb00323245.aspx

please have a look.

also try replacing "If Me.fileUpload.PostedFile Is Nothing Then" with "If fileUpload.PostedFile Is Nothing Then"

and check permissions on the destination folder

DmitryK
permissions are fine. Made your changes and that msdn tutorial doesnt work either. Also, very hard to trust it, when they have <INPUT type=file and <input type="button in the same code block. :-/
madphp
A: 

Try removing the enctype="multipart/form-data" from the form tag. I'm looking at my pages that I use the upload on and they don't have it.

I have the form tag in a master page, but it's just:

< form id="form1" runat="server" > < form >

twlichty
that didnt do it. Im nearly sure that is required. Either way, didnt do it. hmmmm
madphp
fixed it. There was a <form> tag in the master, so the form I added below was nested. I removed the form tag from the master. Would that cause problems elsewhere. Should I just remove the form tag above instead of the master.
madphp
A: 
    Public Sub UploadFile(ByVal BugID As System.Guid, ByVal Files As System.Web.UI.WebControls.FileUpload, ByVal fileDescription As String)
        Dim guid As System.Guid = System.Guid.NewGuid()
        Dim filesSave As New BugTrackerData.Files.Files()
        Dim filename As String = Files.PostedFile.FileName
        'Grab the file name from its fully qualified path at client 
        Dim strFileName As String = guid.ToString() & System.IO.Path.GetExtension(filename)
        'Save uploaded file to server at C:\ServerFolder\
        Dim savepath As String = System.Web.HttpContext.Current.Server.MapPath("~/Resources/FileUploads/" & strFileName)
        Try
           If Not String.IsNullOrEmpty(FileUpload1.FileName) Then
 Files.PostedFile.SaveAs(savepath)
            filesSave.SaveToDB(guid, BugID, strFileName, fileDescription)
End If
        Catch Exp As Exception
            Throw Exp
        End Try
    End Sub
andrewWinn
+1  A: 

Try using:

If Me.fileUpload.HasFile Then        
    Response.Write("You must specify file to upload!") 
Else
TGnat
A: 

fixed it. There was a tag in the master, so the form I added below was nested. I removed the form tag from the master. Would that cause problems elsewhere. Should I just remove the form tag above instead of the master.

ps I hate vb.net and everything about it.

madphp
Yes, just have the form tag in the master page.
twlichty
ASP.net needs a form in every page to do the magic. Just keep the form in the master page, and remove the child one.
Eduardo Molteni
BTW, it is not a VB.net thing, it is ASP.net
Eduardo Molteni