-1

I'm using jQuery plugin with $.ajax() functions. There is some way to upload a file with POST and do some function when is it done? here my code.... what i do wrong?

$.ajax({
                url: "../index.php?rqst=upload",
                type: 'POST',
                data:{
                    'fileToUpload': $("#fileToUpload").val()

                },
                success: function(data) {
                    alert("file upload");
                }}); 

and this is the file input:

<input class="form-control" type="text" name="fileToUpload" id="fileToUpload">

and there is a way to display a gif during the load??? thanks in advance

r1si
  • 1,136
  • 2
  • 19
  • 34

3 Answers3

2

File uploads must be enctype='multipart/form-data' and you can't mix GET and POST like you're doing. Since this is POST, remove the ?rqst=upload from the url. Also $("#fileToUpload").val() is only going to give you the name of the file, not the actual file. You need a specialized javascript library to do file uploads with Ajax. Also, for a file upload, your input's type must be type='file' not type='text'.

developerwjk
  • 8,619
  • 2
  • 17
  • 33
1

It's much easier to use $.post() than $.ajax().

You have it almost right. Instead of success use complete.

Just before running $.ajax(), add some simple dom manipulation code that puts a spinning gif somewhere. Then in the success callback handler add code to remove it.

Also, there is a javascript library for making spinning loading icons: http://fgnass.github.io/spin.js/#!

Mikayla Maki
  • 473
  • 6
  • 18
1

firstly i would check you are able to connect to the file directly, comment out your data section and the change the url to a page holding an echo only. then if the page can be found connected to you will get the alert.

on another note ensure "index.php?rqst=upload" has an echo in it so the success function picks it up. i usually use echos within if else statements so i can see if an error occurs and what error, then i can use the js to display the necessary message

JParkinson1991
  • 1,256
  • 1
  • 7
  • 17