How to Upload file onclick of button of type=button not type=submit in mvc4 using html begin form

2.9k views Asked by At

I have to upload a file using a button (of type button, not submit).

And I want to show popup when no-file is selected and clicked on upload, it should not go to controller action.

Can anyone help please?

1

There are 1 answers

1
Razack On BEST ANSWER
@using(Html.BeginForm("Upload","Home",new {@id="frm"}))
{
    <input type="file" id="upld" />
    <input type="button" value="upload" id="btn" />
}

<script>
  $('#btn').click(function(){
     var has_selected_file = $('#upld').filter(function(){
          return $.trim(this.value) != ''
     }).length  > 0 ; 
     if(has_selected_file){
          $('#frm').submit();
      }
      else{
          alert('No file selected');
      }
  });

I hope this is your requirement