13

As we all know, .serialize() and .serializeArray() conveniently misses file fields (also `hidden fields).

But, Is there a work around from which I could get the value of the file field from .serialize()?

P.S: I just intend to get the value, not upload the file.(Of Course, Uploading file would be better if it is possible, but I dont think JS is capable of such action)

Starx
  • 77,474
  • 47
  • 185
  • 261
  • 1
    `.serialize()` can't work on file fields, because JavaScript has no access to it's content (that is the content of the file). It's simply not possible to submit files with AJAX. – RoToRa Apr 01 '11 at 14:20
  • Not anymore : https://developer.mozilla.org/en/docs/DOM/FileReader – luxcem Sep 03 '13 at 15:01

1 Answers1

4

You may checkout the jquery form plugin which supports file uploads as well.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • This might be an alternate, but still I would like just to get the value in the file field. – Starx Apr 01 '11 at 12:51
  • @Starx, have you tried `$('#id_of_your_file_input').val()`? Obviously you shouldn't forget that for security reasons you won't get the path to the file, but only the filename. – Darin Dimitrov Apr 01 '11 at 12:52
  • @Darin, I guess you forgot about my question, when you suggested `$('#id_of_your_file_input'). val()`. I want the `.serialize()` to be able to get the `$('#id_of_your_file_input'). val()` – Starx Apr 01 '11 at 12:57
  • 2
    @Starx, no I didn't forget about your question. I thought that you could `$('form').serialize() + '&foo=' + $('#id_of_your_file_input').val()` or simply add normal hidden field to your form and use my suggested code to populate its value. – Darin Dimitrov Apr 01 '11 at 12:59
  • @Darin, Thanks, for another alternative. For a simple case, it works, but it my case, even the fields are not known, coz they are dynamically generated and at what number is also not known. So it really does not work for me without resolving to ugly complexions. – Starx Apr 01 '11 at 14:10
  • @DarinDimitrov Do you mind adding an example of plugin usage to the answer? – Babken Vardanyan Jun 25 '18 at 16:22