[英]mvc 2 upload with vb.net
I didn't find any answer on web with vb.net (I find millions of samples with c#) 我在vb.net上没有在网上找到任何答案(我在C#中找到了数百万个示例)
I translated a sample controller to vb.net but doesn't upload 我将示例控制器翻译为vb.net,但未上传
My ResimController.vb 我的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
The Anasayfa.aspx 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)%>
<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. 视图开始处的AjaxControlToolkit注册和表单上缺少的action属性引起了对设计的严重怀疑。 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. 似乎您正在尝试使用ASP.NET MVC中的禁忌服务器端控件,因为它们依赖于viewstate和postback模型。
Here's an example of how you could implement file uploads in MVC: 这是如何在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 ------------- -----------已解决的问题:将enctype =“ multipart / form-data”添加到了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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.