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
<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:
View: