I didn't find any answer on web with vb.net (I find millions of samples with c#)
I translated a sample controller to vb.net but doesn't upload
My ResimController.vb
开发者_C百科<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
The Anasayfa.aspx page
<%@ 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)%>
<input id="Resimx" name="Resimx" type="file" />
<br />
<br />
<input id="Submit1" type="submit" value="submit" />
</form>
</asp:Content>
The AjaxControlToolkit registration in the beginning of your view and the missing action attribute on your form raise some serious suspicions about your design. It seems that you are trying to use server side controls which is a no-no in ASP.NET MVC because they rely on viewstate and postback model.
Here's an example of how you could implement file uploads in 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
View:
<% 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 %>
I am removing master page from project and uploading. I dont know what is wrong in my master page.. This is my master page..
----------- Problem Fixed : added enctype="multipart/form-data" to form1 -------------
<%@ 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>
精彩评论