上传表单(.vbhtml)和VB控制器

时间:2013-11-14 14:07:44

标签: vb.net razor

我是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

如果我错过了什么,请告诉我。

1 个答案:

答案 0 :(得分:0)

这就是我解决它的方式。
:控制器

'文件上传

_ 公共功能索引(文件为HttpPostedFileBase)作为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>