0

我是 vb 编程的新手,一直在尝试实现一个上传表单来将文件上传到我的网络服务器。我正在使用 .vbhtml 和 VB 。我已经创建了表单,但是当我单击提交按钮 "Button1_Click" 时,什么也没有发生。这是我的代码

---- Index.vbhtml 代码 -----

@Code
    ViewData("Title") = "Home Page"
 End Code  


    <form id="form1" runat="server" action="">
        <div>
            <input type="file" ID="FileUpload1" runat="server" /><br />
            <br />
            <input type="button" ID="submit_button" runat="server" OnClick="Button1_Click" 
             value="Upload File" />&nbsp;<br />
            <br />
             <span id="Span1" runat="Server" />
             </div>
      </form> 

-------- HomeController.vb中使用的代码--------

Public Class HomeController

Inherits System.Web.Mvc.Controller


Dim Span1 As Object
Dim FileUpload1 As Object




'Upload file

Public Sub Button1_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs)

    If FileUpload1.HasFile Then
        Try
            FileUpload1.SaveAs("C:\inetpub\wwwroot\Uploads\" & _
               FileUpload1.FileName)
            Span1.Text = "You have specified a file."
            Span1.Text = "File name: " & _
              FileUpload1.PostedFile.FileName & "<br>" & _
               "File Size: " & _
              FileUpload1.PostedFile.ContentLength & " kb<br>" & _
               "Content type: " & _
               FileUpload1.PostedFile.ContentType
        Catch ex As Exception
            Span1.Text = "ERROR: " & ex.Message.ToString()
        End Try
    Else
        Span1.Text = "You have not specified a file."
    End If
End Sub

End Class

如果我遗漏了什么,请告诉我。

4

1 回答 1

0

这就是我解决它的方法..
:控制器

'上传文件

_ Public Function Index(file As HttpPostedFileBase) As ActionResult

If file.ContentLength > 0 Then
    Dim fileName = IO.Path.GetFileName(file.FileName)
    Dim path__1 = IO.Path.Combine(Server.MapPath("~/Uploads"), fileName)
    file.SaveAs(path__1)
End If
Debug.WriteLine("File uploaded")
Return RedirectToAction("Index")

结束功能

:index.vbhtml

<form action="" method="post" enctype="multipart/form-data">

  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
    <input type="submit" />

</form>
于 2013-11-15T10:49:56.687 回答