0

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());
       }
   });
   });
0

3 Answers 3

1

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());
       }
    });
Sign up to request clarification or add additional context in comments.

Comments

1

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());
    });
  }
});

Comments

1

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());
   }
});

2 Comments

I want to send the file name if the browse button is clicked and file name is selected..
But those are two different events.. Click occurs after browse is clicked but before file is selected..

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.