0

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. 
1
  • You're looking to asynchronously send the file to the server using AJAX. Commented Nov 18, 2011 at 18:56

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes.. I went through that blog post. Always my file is always empty.
@Bhuvana, well then maybe you would like to show us your code? The example in the blog post works perfectly fine so it must be you that are doing something wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.