2011-11-18 102 views
0

enter code here我想用FileUpload Control上傳文件。由於在我的表單中有太多的複雜性,我不能使用Html.BeginForm(當然這很好)。我想要做的就是使用Input id調用發送文件到控制器的函數。我在網上搜索,但我的需要是這樣的。我不想提交整個表格。我想要做的就是上傳文件並復原到我的表單以完成剩餘的字段。任何類型的代碼片段將不勝感激。由於ASP.NET MVC2中的FileUpload控件C#

下面是代碼示例我有什麼

<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. 
+0

您正在尋找**異步**使用** AJAX **將文件發送到服務器。 – Amy

回答

1

我想上傳使用FileUpload控件

不要嘗試這個做這個文件。閱讀following blog post,它解釋瞭如何在ASP.NET MVC應用程序中上傳文件。

+0

是的,我經歷了那篇博文。總是我的文件總是空的。 – Bhuvana

+0

@Bhuvana,那麼也許你想向我們展示你的代碼?在博客文章中的例子非常好,所以一定是你做錯了什麼。 –