2011-01-11 102 views
0

好的。上傳文件MVC

我在想我。我想如何將文件上傳到服務器上,但我無法解決。

我讓代碼,任何想法。

<%@ 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 always is Nothing 
    ' Continue code 
End Function 

問候。

+0

任何錯誤? .... – 2011-01-11 22:06:34

回答

0

好的。

問題是我使用的是MasterPages,所以在MasterPage中我已經有一個標籤< form>。我剛ENCTYPE =「多部分/格式數據」添加到主形態和次視圖

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

謝謝大家刪除過載BeginForm。我希望這種幫助別人

1

您的文件輸入被稱爲abc因此同樣應該是你的行動的說法:

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 

而且你使用的是BeginForm方法的錯誤超載。它應該是this one

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

請注意參數是如何反轉的。

您還可以從Phil Haack結賬this blog post

+0

我的錯誤。實際上Action中的參數名稱是abc。那是因爲我嘗試了很多東西。但是abc總是沒有什麼 – 2011-01-11 22:14:30

2

您可以使用下面的代碼到你的觀點:

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

其中文件1是HttpPostedFileBase對象。

原始來源上snip2codeUpload File