2

How do I get upload file name after click the submit button. Below in the file upload field.

<input class="required-field" type="file" value="" name="cover_image">

I've tried with below but it not get the value.

var filename = $("[name='cover_image']", form).val();

Here I don't have ID fro the input filed but is it possible to get upload file name?

Miuranga
  • 2,267
  • 9
  • 46
  • 76
  • what is `form`? a var or the actual `form` tag. – Jai Jun 25 '14 at 07:41
  • I use `form` for get the value in filed. ex: for get text field value I've used `var magcode = jQuery("[name='magcode']", form).val();` – Miuranga Jun 25 '14 at 07:45
  • 1
    This question has been answered before: http://stackoverflow.com/questions/6365858/use-jquery-to-get-the-file-inputs-selected-filename-without-the-path – Khalid T. Jun 25 '14 at 07:47
  • 1
    @miuranga your `form` is a variable, we don't know what its value is. – billyonecan Jun 25 '14 at 07:49

2 Answers2

1

You will get the full path using .val() for getting filename you can use:

 $('[name="cover_image"].required-field').val().split('\\').pop();
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114
1

Try this instead:

var filename = $("[name='cover_image']", 'form').val();

or one suggestion is to use form's id here:

var filename = $("[name='cover_image']", "formID").val();

or you can try this too:

var form = $('YourForm');
var filename = form.find("[name='cover_image']").val();

you can use form because this is the jQuery object and you can find the specific element in it.

Jai
  • 71,335
  • 12
  • 70
  • 93