2010-10-27 4 views
1

Je ne trouve pas de réponse sur le Web avec vb.net (je trouve des millions d'échantillons avec C#)2 mvc télécharger avec vb.net

Je traduisais un contrôleur échantillon à vb.net mais ne télécharge pas

Mon 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 

La page 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> 

Répondre

1

Le AjaxControlToolkit regis La description au début de votre vue et l'attribut d'action manquant sur votre formulaire soulèvent de sérieux soupçons sur votre conception. Il semble que vous essayez d'utiliser les contrôles côté serveur, ce qui est un non-non dans ASP.NET MVC, car ils reposent sur le modèle viewstate et postback.

Voici un exemple de la façon dont vous pouvez implémenter le téléchargement de fichiers dans MVC:

Controller:

<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 

Vue:

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

Merci pour la réponse Darin. Renvoie Rien de nouveau. – Ramazan

+0

Qu'est-ce qui ne retourne rien? Pourriez-vous être un peu plus précis? J'ai testé mon exemple de code avant de l'afficher et cela a bien fonctionné. –

+0

http://img24.imageshack.us/img24/444/nothingt.jpg this. – Ramazan

0

je retire la page principale du projet et le téléchargement. Je ne sais pas ce qui ne va pas dans ma page principale .. C'est ma page principale ..

Problème ----------- Correction: ajout enctype = "multipart/form-data" pour former1 -------------

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