0

i am using jquery fileupload and i want to pass the input name of the file in the formData and everything i have try fails.

I try to use this.name but its seems that i dont have access of this in the formData

formData : { action:'upload', field:this.name , extensions : 'gif|jpeg|jpg|png'}

I have try to use data attributes but i dont get any post in that case

<input id="input-image2" class="file-upload" type="file" name="image2" 
data-form-data='{ action:'upload', field:image2 , extensions : 'gif|jpeg|jpg|png'}'
>

Does anyone know how to add the input name (in this case image2) to formData?

The fileupload init as follows

and i need to fill the field param of formData with the name of the input file which is image2

$('.file-upload').fileupload({
        url: '/myfile.php',

        formData : { action:'upload', field:this.name , extensions : 'gif|jpeg|jpg|png'},

        acceptFileTypes: /(\.|\/)(gif|jpeg|jpg|png)$/i,

        add : function (e, data) { 


            var type=data.files[0].type;
            var size=data.files[0].size;
            var name=data.files[0].name;

            if(type=="image/gif" || type=="image/jpg" || type=="image/jpeg" || type=="image/png") 
            {
                data.submit();
            }
            else 
            {
                //data.submit();
                alert('Please upload only .gif, .jpeg, .jpg .png files');
            }
        },                  
        fail: function(e, data){
            alert(data.jqXHR.responseText);      
        },

        dataType: 'json',
        done: function (e, data) 
        {
            //code here
        },
        progressall: function (e, data) 
        {

            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .progress-bar').css(
                'width',
                progress + '%'
            );


        }
    }).prop('disabled', !$.support.fileInput)
        .parent().addClass($.support.fileInput ? undefined : 'disabled');

Any help appriciated. Thanks

ntan
  • 2,195
  • 7
  • 35
  • 56

1 Answers1

0

Just access the input element by id and use the attr function on it to get the name attribute:

formData : { action:'upload', field:$("#input-image2").attr("name"), extensions : 'gif|jpeg|jpg|png'};
eol
  • 23,236
  • 5
  • 46
  • 64
  • i dont know the id (input-image2) and i a have to find it programatically – ntan Jun 29 '16 at 10:08
  • http://stackoverflow.com/questions/3577469/form-onsubmit-determine-which-submit-button-was-pressed or http://stackoverflow.com/questions/3577469/form-onsubmit-determine-which-submit-button-was-pressed may help then. – eol Jun 29 '16 at 10:21