2013-11-14 8 views
0

我是vb編程新手,一直在嘗試實現上傳表單以將文件上傳到我的網絡服務器。我使用.vbhtml和VB。我創建了表單,但是當我點擊提交按鈕「Button1_Click」時,沒有任何反應。這裏是我的代碼上傳表單(.vbhtml)和VB控制器

---- Index.vbhtml代碼-----在HomeController.vb使用

@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> 

-------- --------代碼-----------

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 

請讓我知道如果我失去了一些東西。

+0

您正在混合使用MVC和WebForms ... –

回答

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>