6

I am attempting to submit a form via jQuery. My form contains fields and a file that must be uploaded. It is of type ENCTYPE="multipart/form-data".

I can receive all my field values using: post = $('#myForm').serialize(); But how do I receive the $_FILES array? I need this to process the uploaded file.

Is this possible using jQuery, and if so how? Or do I need to use a special upload plugin for jQuery?

6 Answers 6

12

jquery form is the best way to do it, you can add it to any normal form,

<form method="post" action="URL">
<input type="file" name="file">
<input type="text" name"text">
<input type="submit"> 
</form>

<script type="text/javascript">
$(document).ready(function() { 
  $(form).ajaxForm();
})
</script>

will work as expected, but with ajax.

http://malsup.com/jquery/form/#code-samples

Sign up to request clarification or add additional context in comments.

Comments

11

You cannot upload files through javascript.

Check out this related question:
Is it possible to use Ajax to do file upload?

Essentially, the two most popular ways of "faking" AJAX for file uploads is using a Flash plugin such as SWFUpload or submitting the form to a hidden iframe that processes the request.

Comments

1

Form contains a file input, but is missing method=POST and enctype=multipart/form-data on the form. The file will not be sent

Comments

1

Use FormData

<form>
<label for="imageToSend">Cargar imagen a la galeria</label>
<input type="file" name="imageToSend" id="imageToSend" value="Cargar imagen" />
</form>
<script>
$('#imageToSend').on('change',function(event){
    var dialog = $('#dialog');
    var Data = new FormData();
    Data.append('imageToSend',$('#imageToSend')[0].files);
    $(this).val('');//Clear input file
    $.ajax({
        url: "/upload",
        data: Data,
        processData: false,
        contentType: false,
        type:'POST',
        success: function(data){
            if(data.success){
                //success handler   
            }else if(!data.success){
                //error backend handler
            }
        },
        error: function(data){
            //error handler Ej:404 status
        }
    })
  });
</script>

Comments

0

If you can control the environment, like, say, you're writing an admin app for an intranet in which you recommend the browser, then real AJAX file uploads are possible with Firefox 3 and above. In all other cases, the iframe workaround or a Flash based uploader is the way to go.

Comments

0

It's possible, but not working in Google Chrome ) Look!

...
<form method='post' enctype='multipart/form-data'>
<input type="file" id="imf" name="imf"/>
<input type="button" id="Save"/>
</form>

...

$("#Save").live("click", function(){

var photo = document.getElementById("imf"); 
var file  = photo.files[0];

   $.post('/user/saveNewPhoto', {'imf':file.getAsDataURL(), fname:file.fileName }, function( data ){
   alert ( data );
    });

});

upload side script need decode base64 ) and that is all but i don't test this script on big size file

Comments

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.