0

I am trying to send an image through a resource and recovery in a php file but I have not succeeded, this is my JS file:

//* AJAX *//
startAsyncNews: function(){
    if(this.sendimage){
        var formdata = new FormData();
        formdata.append("file",this.contentnew.imageFile );
        console.log(this.contentnew.imageFile);
    }   

    // POST /someUrl
    this.$http.post('controllers/newsController.php', { 
        data:{action : this.accion_new, data_new: this.contentnew , imgf : formdata}
    }).then(response => {
    }, response => {
        console.log("error");
    });
},
imageSelect: function($event){
    this.sendimage=true;
    this.contentnew.imageFile =$event.target.files[0];  
}

When I use the console.log = console.log (this.contentnew.imageFile), it shows me the properties of the image correctly, that is, it is sending the file well, but when I receive it in php and I do vardump I get this object ( stdclass) # 3 (0) no properties no properties and with json_decode / encode I get it empty, also try

headers: {
    'content-type': 'multipart/form-data'
}

But it generates the following error:

Missing boundary in multipart/form-data POST

2 Answers 2

2

You need to add all your data in formdata Object using formdata.append(key,value) function.
Then you simply send formdata

formdata.append('action ',this.accion_new);
formdata.append('data_new',this.contentnew);

this.$http.post('controllers/newsController.php', { 
    data:formdata
});
// or just if i'm not mistaken
this.$http.post('controllers/newsController.php',formdata);

object in http request data.

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

7 Comments

in php var_dump get this array(0) { } object(stdClass)#1 (1) { ["data"]=> object(stdClass)#2 (0) { } } object(stdClass)#2 (0) { }
Can you try the second example ? and try to dump $_FILES.
return this :( ... array(0) {} No properties
can you try the same thing but without any change in headers object ?
I'm not including the headers object :(
|
0

I don't know what this.accion_new and this.contentnew are, but this line:

this.$http.post('controllers/newsController.php', { 
    data:{action : this.accion_new, data_new: this.contentnew , imgf : formdata}
})

should simply be be:

  this.$http.post('controllers/newsController.php', formdata)

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.