ASP.NET MVC2中的FileUpload控件C#

时间:2011-11-18 18:34:03

标签: asp.net asp.net-mvc-2 file-upload

enter code here我正在尝试使用FileUpload Control上传文件。由于我的表单中有如此多的复杂性,我不能使用Html.BeginForm(当然这完全没问题)。我想做的就是使用Input id调用函数将postedfile发送到控制器。我在网上搜索,但我需要的是这个。我不想提交整个表格。我想要做的就是上传文件并回到我的表单来完成其余的字段。任何类型的代码片段都将受到赞赏。感谢

以下是我所拥有的代码示例

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    ViewReportFiles
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<% using (Html.BeginForm([ActionName], [ControllerName], FormMethod.Post, new { target = "_blank" }))
     {%>
Here I have few fields  to process.
Along with this I have fileUpload control

    <table>
    <tr>
       <td valign="bottom">
        <input type="file" id="document" name="document" accept="text/xml, text/csv, application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" size="76" />
        &nbsp;&nbsp;&nbsp;&nbsp;
      </td>

      <td valign="bottom">
      <div Id="UploadFile">    <input type="image" alt="upload file" src="<%= ResolveUrl("~/media/form_upload_btn.png") %>" name="image"  /> </div>
      </td>
    </tr>
  </table>


<% }>

My javascript is as follows
   <script type="text/javascript" language="javascript">
       $("#UploadFile").click(function () {

           if ($("#document").val() == '') {
               // checking for selected file
               alert('Please select a document.');
               return false;
           }
           $.ajaxFileUpload({
               **url: '<%= Url.Action("actionName", "Controller") %>',**
               data: { val: 'aaa' },
               secureuri: false,
               fileElementId: 'document',
               dataType: 'xml',
               success: function (data) {

               }

           });
       });
   </script> 

Here the problem is it's not getting into that URL. it goes to my HTTPPost of the page.
My controller code is like this.... 

 public void UploadAccessDataFile(){

 foreach (string uploadFile in Request.Files)
           {
        }
}


Please help. 

1 个答案:

答案 0 :(得分:1)

  

我正在尝试使用FileUpload Control上传文件

不要试图这样做。阅读following blog post,其中解释了如何在ASP.NET MVC应用程序中上传文件。