开发者

Upload File MVC

开发者 https://www.devze.com 2023-02-04 17:57 出处:网络
Ok. I\'m loosing my mind. I think understood how to upload files onto a server but I can not work it out.

Ok.

I'm loosing my mind. I think understood how to upload files onto a server but I can not work it out.

I let the code, any ideas.

<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Cintas.Master" Inherits="System.Web.Mvc.ViewPage(Of iABC.Temp)" %>

<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolderContenido" runat="server">
<%=Html.ValidationSummary("Se produjeron  errores al procesar la solicitud. Revise los errores e intente nuevamente.")%>

<% Using (Html.BeginForm("CargarArchivos", "ProcesosAutomaticos", FormMethod.Post, New With {.enctype = "multipart/form-data"}))%>
    <input type="file" name="abc" id="abc" />
    <input type="submit" value="Submit Button" />
<% End Using%>
</asp:Content>  



<AcceptVerbs(HttpVerbs.Post)> _  
Function CargarArchivos(ByVal abc As HttpPostedFileBase) As ActionResult  
  ' But abc a开发者_高级运维lways is Nothing  
  ' Continue code  
End Function  

Regards.


You can use the following code into your View:

<div id="Componentsdiv">
@using (Html.BeginForm(<action>, <Controller>, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <fieldset>
        <legend>Upload TXT File</legend>

        <div class="editor-label">
            <fieldset>
            <legend>File</legend>
                @Html.TextBoxFor(m => m.File1, "Select new file", new {  id="firstfile", type = "file"})
                @Html.ValidationMessageFor(m => m.File1)
            </fieldset>
        </div>
        <p>
            <input type="submit" id="submitbutton" hidden="hidden"/>
        </p>
    </fieldset>
}
</div>

Where File1 is a HttpPostedFileBase object.

Original Source on snip2code: Upload File


Your file input is called abc so the same should be your action argument:

Function CargarArchivos(ByVal abc As HttpPostedFileBase) As ActionResult  
    If abc IsNot Nothing AndAlso abc.ContentLength > 0 Then
        abc.SaveAs("C:\" & "a.txt")  
    End If  
    Return View()  
End Function 

Also you are using a wrong overload of the BeginForm method. It should be this one:

<% Using (Html.BeginForm("CargarArchivos", "ProcesosAutomaticos", Nothing, FormMethod.Post, New With { .enctype = "multipart/form-data" }))%>

Notice how the arguments are inverted.

You might also checkout this blog post from Phil Haack.


Ok.

The problem was I'm using MasterPages, so in the MasterPage I already have a tag < form>. I just add enctype = "multipart/form-data" to the main form and delete overload BeginForm from the secondary view

<% Using (Html.BeginForm("CargarArchivos", "ProcesosAutomaticos", New With {.enctype = "multipart/form-data"}, FormMethod.Post))%>  

Thanks everybody. I hope this help someone else

0

精彩评论

暂无评论...
验证码 换一张
取 消