Welcome Guest, Not a member yet? Register   Sign In
Can't upload file [CI4]
#1

Hello! 
I'm trying to upload a file through a simple form, but when it gets to retrieve it on server side the corresponding parameter is empty.
I tried to do the upload without using CI with a basic form and a basic server side script and it works, so maybe there's something wrong in CI... what could it be?

I already checked field name, write permission on folder (which is the same I used with the basic script), form enctype.... everything!
Any ideas or suggestions?
Reply
#2

Try this:
https://codeigniter4.github.io/CodeIgnit...he-process
Reply
#3

(This post was last modified: 11-29-2023, 09:20 AM by cellomar.)

(11-28-2023, 10:48 PM)kenjis Wrote: Try this:
https://codeigniter4.github.io/CodeIgnit...he-process

Thanks, I've already tried that code and the img variabile is null when it comes to call its methods. I found that the issue is that I'm making an asynchronous call to the controller that saves data, turning it to synchronous make it works.
So now the question is: what has to be done to make it work with an ajax call?

(11-29-2023, 08:48 AM)cellomar Wrote:
(11-28-2023, 10:48 PM)kenjis Wrote: Try this:
https://codeigniter4.github.io/CodeIgnit...he-process

Thanks, I've already tried that code and the img variabile is null when it comes to call its methods. I found that the issue is that I'm making an asynchronous call to the controller that saves data, turning it to synchronous make it works.
So now the question is: what has to be done to make it work with an ajax call?

Here is js code I'm using


Code:
$("#myform input").each( function(indice, elem){

        if($(elem).prop("type") == "checkbox" && $(elem).is(":checked")){
           
            formData.append($(elem).attr("name"), $(elem).val());

        }

        else{

          if($(elem).prop("type") != "checkbox")
            formData.append($(elem).attr("name"), $(elem).val()); 
        }
    });
   

    $("#myform select").each( function(indice, elem){

        formData.append($(elem).attr("name"), $(elem).val());
    });

   
    $.ajax( {
       
        url: urlbase + "public/struttura/salva_struttura",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false
   
    }).done(function(data){

        if(data.success == true){
            alert("salvataggio riuscito");
        }
Reply
#4

(11-29-2023, 08:48 AM)cellomar Wrote:
(11-28-2023, 10:48 PM)kenjis Wrote: Try this:
https://codeigniter4.github.io/CodeIgnit...he-process

Thanks, I've already tried that code and the img variabile is null when it comes to call its methods. I found that the issue is that I'm making an asynchronous call to the controller that saves data, turning it to synchronous make it works.
So now the question is: what has to be done to make it work with an ajax call?

(11-29-2023, 08:48 AM)cellomar Wrote:
(11-28-2023, 10:48 PM)kenjis Wrote: Try this:
https://codeigniter4.github.io/CodeIgnit...he-process

Thanks, I've already tried that code and the img variabile is null when it comes to call its methods. I found that the issue is that I'm making an asynchronous call to the controller that saves data, turning it to synchronous make it works.
So now the question is: what has to be done to make it work with an ajax call?

Here is js code I'm using


Code:
$("#myform input").each( function(indice, elem){

        if($(elem).prop("type") == "checkbox" && $(elem).is(":checked")){
           
            formData.append($(elem).attr("name"), $(elem).val());

        }

        else{

          if($(elem).prop("type") != "checkbox")
            formData.append($(elem).attr("name"), $(elem).val()); 
        }
    });
   

    $("#myform select").each( function(indice, elem){

        formData.append($(elem).attr("name"), $(elem).val());
    });

   
    $.ajax( {
       
        url: urlbase + "public/struttura/salva_struttura",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false
   
    }).done(function(data){

        if(data.success == true){
            alert("salvataggio riuscito");
        }

Mistery solved, there was an operation missing on js side to retrieve file content, here it is


Code:
let file_data = $('input[name="userfile"]')[0].files;
formData.append('userfile', file_data[0]);
Reply




Theme © iAndrew 2016 - Forum software by © MyBB