简体   繁体   中英

vb.net upload file problem

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

Try using:

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

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

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 >

    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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM