2010-10-27 48 views
1

我沒有找到與vb.net網絡的任何答案(我發現數以百萬計的樣品與C#)MVC 2的上傳與vb.net

我翻譯了樣品控制器vb.net,但不會上傳

我ResimController.vb

<AcceptVerbs(HttpVerbs.Post)> 
Public Function Anasayfa(ByVal forms As FormCollection) As ActionResult 

    Dim errors As Boolean = False 
    If String.IsNullOrEmpty(forms("Resimx")) Then 
     errors = True 
     ModelState.AddModelError("Resimx", "error") 
    Else 
     Dim sFileName As String = forms("Resimx") 

     Dim file = Request.Files("Resimx") 
     ''file' is always null, and Request.Files.Count is always 0 ??? 
     If file IsNot Nothing Then 'This line always returns Nothing 
      Dim buf As Byte() = New Byte(file.ContentLength - 1) {} 
      'do stuff with the bytes 
      file.InputStream.Read(buf, 0, file.ContentLength) 
     Else 
      errors = True 
      ModelState.AddModelError("Resimx", "error") 
     End If 
    End If 
    If errors Then 
     Response.Write("Failed") 
     Return View() 
    Else 
     Response.Write("Success") 
     Return View() 
    End If 

End Function 

的Anasayfa.aspx頁

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %> 
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    Anasayfa 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <h2>Anasayfa</h2> 
    <form method="post" enctype="multipart/form-data" action=""> 
     <%: Html.ValidationSummary(True)%> 
     <%: Html.ValidationMessageFor(Function(model) model)%> 
     &nbsp; 
     <input id="Resimx" name="Resimx" type="file" /> 
     <br /> 
     <br /> 
     <input id="Submit1" type="submit" value="submit" /> 
     &nbsp; 
    </form> 
</asp:Content> 

回答

1

的AjaxControlToolkit雷吉斯在您的視圖開始時刻以及表單上缺少的動作屬性會對您的設計產生一些嚴重的懷疑。看起來你正在嘗試使用ASP.NET MVC中的no-no的服務器端控件,因爲它們依賴於viewstate和postback模型。

這裏有一個如何你可以在MVC實現文件上傳的例子:

控制器:

<HandleError()> _ 
Public Class HomeController 
    Inherits System.Web.Mvc.Controller 

    Function Index() As ActionResult 
     Return View() 
    End Function 

    <HttpPost()> 
    Function Index(ByVal Resimx As HttpPostedFileBase) As ActionResult 
     If Not Resimx Is Nothing Then 
      ' A file has been selected => do something with the uploaded file 
     End If 
     Return View() 
    End Function 
End Class 

查看:

<% Using Html.BeginForm("Index", "Home", Nothing, FormMethod.Post, New With { .enctype = "multipart/form-data" })%> 
    <input id="Resimx" name="Resimx" type="file" /> 
    <input type="submit" value="Upload" /> 
<% End Using %> 
+0

感謝您的回答達林。再次返回沒有。 – Ramazan 2010-10-27 16:42:29

+0

什麼都不返回?你能更具體一點嗎?在發佈之前我已經測試了我的示例代碼,它工作正常。 – 2010-10-27 17:03:13

+0

http://img24.imageshack.us/img24/444/nothingt.jpg這個。 – Ramazan 2010-10-27 17:07:14

0

我從項目和上傳除去母版頁。 我不知道什麼是錯誤的,我的母版頁.. 這是我的母版頁..

-----------問題修正:增加ENCTYPE =「的multipart/form-data的」形成1 -------------

<%@ Master Language="VB" Inherits="System.Web.Mvc.ViewMasterPage" %> 


<%-- The following line works around an ASP.NET compiler warning --%> 
<%: ""%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title> 
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" /> 
    <link rel="stylesheet" type="text/css" href="../../Content/jkmegamenu.css" /> 

    <script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script> 

    <script type="text/javascript" src="../../Content/jkmegamenu.js"></script> 

    <script type="text/javascript"> 

     //jkmegamenu.definemenu("anchorid", "menuid", "mouseover|click") 
     jkmegamenu.definemenu("megaanchor", "megamenu1", "mouseover") 
     jkmegamenu.definemenu("Ataturk", "Ataturk1", "mouseover") 
</script> 
    <style type="text/css"> 

     .style2 
     { 
      width: 220px; 
      height: 237px; 
     } 

    </style> 
    </head> 

<body> 
    <form id="form1" enctype="multipart/form-data" runat="server"> 
    <center> 
<div id="banner" align="center" dir="ltr" style="border-width: 0px; width: 1000px; height: 180px; top: 0px; position: relative;"> 
    <img alt="Banner" src="../../Content/banner3.jpg" /></div> 
<div id="menuler" align="center" 
      style="border-width: 0px; width: 1000px"> 
<table class="style1"> 
     <tr> 
      <td width="143px"> 
       <%: Html.ActionLink("Anasayfa", "Anasayfa", "Home")%></td> 
      <td width="143px"> 
       <%: Html.ActionLink("Okulumuz", "Anasayfa", "Okulumuz", New With {.id = "megaanchor"})%></td> 
      <td width="143px"> 
       <%: Html.ActionLink("Personel", "Anasayfa", "Personel")%></td> 
      <td width="143px"> 
       <%: Html.ActionLink("Basında Biz", "Anasayfa", "Basin")%></td> 
      <td width="143px"> 
       <%: Html.ActionLink("Aktiviteler", "Anasayfa", "Aktiviteler")%></td> 
      <td width="143px"> 
       <%: Html.ActionLink("Atatürk Köşesi", "Anasayfa", "Ataturk", New With {.id = "Ataturk"})%></td> 
      <td width="143px"> 
       <%: Html.ActionLink("İletişim", "Anasayfa", "Iletisim")%></td> 
     </tr> 
    </table> 

</div> 
<div align="center" 
      style="width: 1000px; border-width: 0px; "> 

<table style="width: 100%"> 
    <tr> 
    <td style="width:267px" valign="top" align="left"> 
      <asp:Calendar ID="Calendar1" runat="server" BackColor="White" 
        BorderColor="#3366CC" BorderWidth="1px" CellPadding="1" 
        DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" 
        ForeColor="#003399" Height="200px" Width="220px" EnableViewState="False" 
        VisibleDate="2010-10-24"> 
        <DayHeaderStyle BackColor="#99CCCC" ForeColor="#336666" Height="1px" /> 
        <NextPrevStyle Font-Size="8pt" ForeColor="#CCCCFF" /> 
        <OtherMonthDayStyle ForeColor="#999999" /> 
        <SelectedDayStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> 
        <SelectorStyle BackColor="#99CCCC" ForeColor="#336666" /> 
        <TitleStyle BackColor="#003399" BorderColor="#3366CC" BorderWidth="1px" 
         Font-Bold="True" Font-Size="10pt" ForeColor="#CCCCFF" Height="25px" /> 
        <TodayDayStyle BackColor="#99CCCC" ForeColor="White" /> 
        <WeekendDayStyle BackColor="#CCCCFF" /> 
       </asp:Calendar> 

       <br /> 
       <div class="meb"> 

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" 
         codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" 
         class="style2"> 
<param name="allowScriptAccess" value="sameDomain" /> 
<param name="movie" value="http://www.meb.gov.tr/haberler/xml/swf/145-180/145-180-13.swf?param=0" /> 
<param name="quality" value="high" /> 
<embed src="http://www.meb.gov.tr/haberler/xml/swf/145-180/145-180-13.swf?param=0" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="220" height="236" allowscriptaccess="sameDomain"></embed> 
</object> 


      </div> 
       </td> 
    <td valign="top" align="left"> 
       <asp:ContentPlaceHolder ID="MainContent" runat="server" /> 
      </td> 
    <td style="width:267px" valign="top" align="left">E-Okul veli bilgilendirme sistemi<br /> 

      <a target="_blank" href="https://e-okul.meb.gov.tr/Ilkogretim/Veli/iov00001.aspx"><img src="../../Content/e-okul.jpg" alt="E-Okul" /></a><br /> 
      <br /> 
      <br /> 
      MEB Internet TV<br /> 

      <a target="_blank" href="http://internettv.meb.gov.tr/"><img src="../../Content/meb_tv.jpg" alt="Meb TV" /></a><br /> 
      </td> 
    </tr> 
</table> 

</div> 

<div align="center" style="font-size: x-small; color: #C0C0C0"> 
Ramazan AKTOLU 
</div> 
</center> 
<div id="megamenu1" class="megamenu"> 

<div class="column"> 
<h3>Okulumuz</h3> 
<ul> 
<li><%: Html.ActionLink("Okulumuz", "Anasayfa", "Okulumuz") %></li> 
    <li><%: Html.ActionLink("Tarihçe", "Tarihce", "Okulumuz") %></li> 
    <li><%: Html.ActionLink("Vizyon & Misyon", "Vizyon", "Okulumuz") %></li> 

</ul> 
</div> 


</div> 
<div id="Ataturk1" class="megamenu"> 
<div class="column"> 
<h3>Atatürk Köşesi</h3> 
<ul> 
<li><%: Html.ActionLink("Atatürk Köşesi", "Anasayfa", "Ataturk") %></li> 
</ul> 
</div> 
</div> 
    </form> 

</body> 
</html>