Sameeksha Kumari
Sameeksha Kumari

Reputation: 1264

Passing file with filename and other parameters to upload files using jquery

I am using Jquery to upload files. The problem I intend to pass some more parameters with the selected file. But, I experienced that processData attribute value should be set to false if passed data is not to be processed as string. When I do that, I cannot pass any other values with data passed with jquery ajax.

Below is the code I am using:

$.ajax({
    url: 'php/upload.php',
    data: data,         // here I want to pass more data like { filedata:data, op1 : val1, op2:val2 }
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
    success: function(data){
        alert(data);
    }
});

Please share your ideas if you have been able to find a workaround for this situation. Or if you know any other way of passing data. Please share.

Upvotes: 0

Views: 8562

Answers (2)

ngLover
ngLover

Reputation: 4578

You can use iframe for such upload of image with jquery...

Ex.

<iframe  name='Status_update' src='updating.php'' scrolling='no'
      sandbox='allow-forms' frameborder='0' style='height:150px;width:700px;'>
</iframe>
<script>
$(document).ready(function(){
    $("#input_for_image_upload").change(function() //can call on any function
    {
        $("#img_upload").submit();
    });
});
</script>

Upvotes: 0

hari
hari

Reputation: 1963

try this,

var data= false;
if (window.FormData) {
     data= new FormData();
}

if (formdata) {
    data.append("image", file);
    data.append("opt1",'value1');
    data.append("opt2",'value2');
 }

 if (data) {
          jQuery.ajax({
                    url: "php/upload.php",
                    type: "POST",
                    data: data,//Now you attached datas
                    processData: false,
                    contentType: false,
                    success: function (data) {
                       alert("Response  Data : "+data);
                    }
                });
            }

Upvotes: 2

Related Questions