Bhuvana
Bhuvana

Reputation: 1

FileUpload control in ASP.NET MVC2 C#

enter code hereI am trying to upload a file using FileUpload Control. As there are so many complexities within my form, I cannot use Html.BeginForm (of course this works perfectly fine). All I want to do is with Input id call the function to send the postedfile to the controller. I searched in the web, but my need is this. I DO NOT WANT TO SUBMIT THE WHOLE FORM. All I want to do is upload file and comeback to my form to complete the rest of the fields. Any type of code snippet would be appreciated. Thanks

Here is the sample of code what I have

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

Upvotes: 0

Views: 1209

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039508

I am trying to upload a file using FileUpload Control

Don't try this to do this. Read the following blog post which explains how to upload files in an ASP.NET MVC application.

Upvotes: 1

Related Questions