0

I am using a wizard to complete a user profile on my system. In step 7, I use the wizard to load a pdf document. I want the document to load automatically when selected from the imput, but I am having the following complications.

It is worth mentioning that I want to load it with ajax (so that the process is automatic) and send it to php to save it on the server.

The wizard is this (as reference): https://jsfiddle.net/yeyene/59e5e1ya/

My Input:

<div class="box">
    <input type="file" name="file-7" id="file-7" class="inputfile inputfile-6" data-multiple-caption="{count} files selected" multiple />
        <label for="file-7">
            <span></span> 
            <strong>
                <svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/>
                </svg> 
                Find pdf &hellip;
            </strong>
        </label>
</div>

enter image description here

This is my js

$('#file-7').change(function(e){
    var file_data = this.files[0];
    //var file_data = $(this).prop('files')[0];
    console.log(file_data);
        if(file_data != undefined) {
            var datos = new FormData();
            datos.append('sendpdf', true);                  
            datos.append('pdf', file_data);
            $.ajax({
                url:"views/ajax.php",
                method:"POST",
                data:datos,
                contentType:false,
                processData:false,
                beforeSend: function() {
                    $("#loading").removeClass('hidden');
                },
                success: function(regreso){
                    $("#loading").addClass('hidden');

                    console.log(regreso);
                }
            });
        }  
});

This is my ajax.php

if (isset($_POST["sendpdf"])) {
    var_dump($_POST['pdf']);
}

The error:

enter image description here

console.log(file_data);

enter image description here

Thanks for help me.

8
  • What does console.log(file_data); show? Commented Nov 20, 2019 at 0:58
  • @KalimahApps Ok, I already updated the post. Commented Nov 20, 2019 at 2:34
  • I am not sure about the exact issue. Can try to encode the file object? Use this line datos.append('pdf', btoa(file_data)); and in your PHP file you can add base64_decode ( $_POST['pdf']) Commented Nov 20, 2019 at 3:58
  • @KalimahApps Excellent, I already receive the file in php, but I think it arrives empty prntscr.com/pzdg7a Commented Nov 20, 2019 at 4:12
  • Its not empty. It is an object. Try to use json_decode(base64_decode ( $_POST['pdf'])) Commented Nov 20, 2019 at 4:17

1 Answer 1

2

Only I need receive in php with these code:

$_FILES['file']
Sign up to request clarification or add additional context in comments.

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.