user1710288
user1710288

Reputation: 427

upload file name using jquery

I am trying to send uploaded file name to another file if the size of the fileupload text is not zero. I tried below code and it didn't work. I want to send the file name if the browse button is clicked and file name is selected.. help plz?

<input type="file" name="fileupload" id="fileupload" value=""> 


   $(document).ready(function () {
   $("#fileupload").click(function(){

       if(fileupload>0)
       {      
    $.post("ReadExcel.jsp", {"filename": $("#fileupload").val()});
    $.post("ReadExcel.jsp", {"processId": processId });
    alert($("#fileupload").val());
       }
   });
   });

Upvotes: 0

Views: 425

Answers (3)

Xavier
Xavier

Reputation: 1794

Hope this helps..

$("#fileupload").change(function() {
       if($("#fileupload").val().length > 0) {      
           $.post("ReadExcel.jsp", {"filename": $("#fileupload").val()});
           $.post("ReadExcel.jsp", {"processId": processId });
           alert($(this).val());
       }
    });

Upvotes: 1

Michal Klouda
Michal Klouda

Reputation: 14521

I suspect you wanted to trigger your handler on change and you wanted to check if a file was selected:

$("#fileupload").change(function() {
   if($("#fileupload").val().length > 0) {      
       $.post("ReadExcel.jsp", {"filename": $("#fileupload").val()});
       $.post("ReadExcel.jsp", {"processId": processId });
       alert($("#fileupload").val());
   }
});

Upvotes: 1

Barrie Reader
Barrie Reader

Reputation: 10713

Do you not mean [edited for better code]:

$(document).ready(function () {
  $("#fileupload").click(function(){
    var valueLength = $(this).val().length;
    if(valueLength > 0) {      
      $.post("ReadExcel.jsp", {
        "filename": $(this).val(),
        "processId": processId
      },
      success: function(data) {
        alert($(this).val());
      });
    }
  });
});

Or you could alleviate the need for $(document).ready():

$("#fileupload").on('click', function(){
  var valueLength = $(this).val().length;
  if(valueLength > 0) {      
    $.post("ReadExcel.jsp", {
      "filename": $(this).val(),
      "processId": processId
    },
    success: function(data) {
      alert($(this).val());
    });
  }
});

Upvotes: 1

Related Questions