vb.net上传文件问题

时间:2009-08-28 12:05:31

标签: asp.net vb.net file-upload

当我尝试发布文件时,它返回false,即没有附加文件。任何人都可以看到这个有什么问题?或者可能导致它的原因。

<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

5 个答案:

答案 0 :(得分:1)

尝试使用:

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

答案 1 :(得分:0)

这是MSDN的完整工作示例:

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

请看一下。

还尝试用“如果fileUpload.PostedFile Nothing Then”替换“If Me.fileUpload.PostedFile Is Nothing Then”

并检查目标文件夹的权限

答案 2 :(得分:0)

尝试从表单标记中删除 enctype =“multipart / form-data”。我正在查看我使用上传的页面,但他们没有。

我在母版页中有表单标记,但它只是:

&LT; form id =“form1”runat =“server”&gt; &LT;表格&gt;

答案 3 :(得分:0)

    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

答案 4 :(得分:0)

修好了。主人中有一个标签,所以我在下面添加的表格是嵌套的。我从主服务器中删除了表单标签。这会在其他地方引起问题。我应该只删除上面的表单标记而不是主文件。

ps我讨厌vb.net以及它的一切。