Welcome Guest, Not a member yet? Register   Sign In
Performing AJAX-upload with CI and jQuery?
#1

[eluser]Christoffer[/eluser]
Hey,

I want to dynamically send an upload form to CI via jQuery. The form needs to contain the file to be uploaded and the path to save to.

So far my AJAX-call looks like this:

Code:
[removed]
file = $('#file').val();
path = active_dir;

upload_file( file, path );

function upload_file(file, path) {
  $.post("/upload_file/", { file: file, path: path }, function(data) {
    notice(data);
  });
}[removed]

The PHP-side of the script is tested and OK. And the path is processed as it should. But I get an error message from PHP that says that 'No file was selected.'.

How should I fetch the file data from the form?

Thank you,
Christoffer
#2

[eluser]jdfwarrior[/eluser]
[quote author="Christoffer" date="1250510444"]Hey,

I want to dynamically send an upload form to CI via jQuery. The form needs to contain the file to be uploaded and the path to save to.

So far my AJAX-call looks like this:

Code:
[removed]
file = $('#file').val();
path = active_dir;

upload_file( file, path );

function upload_file(file, path) {
  $.post("/upload_file/", { file: file, path: path }, function(data) {
    notice(data);
  });
}[removed]

The PHP-side of the script is tested and OK. And the path is processed as it should. But I get an error message from PHP that says that 'No file was selected.'.

How should I fetch the file data from the form?

Thank you,
Christoffer[/quote]

I was playing around with the uploader class earlier this morning and ran into the same issue. My problem was though, if you check the documentation on the uploader class, you'll see that by default, the uploader class is expecting a field named 'userfile'. From your code, it appears that your file browser field is named "file" and you are also passing it as "file". If you are doing this, check to make sure that in your uploader code, that you are specifying that it should look for the "file" field. You pass that to the do_upload function. Check the documentation on it. I would bet that's your problem. I didn't notice it at first either.
#3

[eluser]Christoffer[/eluser]
Thank you for your time. But I have tested this form the usual way (by submitting it). However I would like the AJAX-effect and never leave the page.
#4

[eluser]jdfwarrior[/eluser]
[quote author="Christoffer" date="1250547858"]Thank you for your time. But I have tested this form the usual way (by submitting it). However I would like the AJAX-effect and never leave the page.[/quote]

I throw together a form and see if I can get it working really quick
#5

[eluser]Johan André[/eluser]
As far as I know, you can't save files via AJAX.
That is because javascript can only post the $_POST-array. Files are stored in $_FILES-array.
There are some methods involving flash or iframes around.
#6

[eluser]jdfwarrior[/eluser]
[quote author="Johan André" date="1250550539"]As far as I know, you can't save files via AJAX.
That is because javascript can only post the $_POST-array. Files are stored in $_FILES-array.
There are some methods involving flash or iframes around.[/quote]

Didn't think about that initially but was coming back to mention that ($_FILES) array.. You beat me to it ha. I haven't dug into the code yet, but there are a few plugins for jQuery that do allow you to do an ajax upload somehow.. I may go look through those..
#7

[eluser]Johan André[/eluser]
I use "Uploadify" which is really great once you get a hang of it!
I uses flash though, but it has some great features, like the select & upload multiple files with que.




Theme © iAndrew 2016 - Forum software by © MyBB