我试图通过资源发送图像并在 php 文件中进行恢复,但我没有成功,这是我的 JS 文件:
//* 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];
}
当我使用console.log = console.log (this.contentnew.imageFile)
时,它正确地显示了图像的属性,也就是说,它很好地发送了文件,但是当我在 php 中收到它并且我执行 vardump 时,我得到这个对象( stdclass) # 3 (0) 没有属性没有属性,并且使用 json_decode/encode 我得到它空,也尝试
headers: {
'content-type': 'multipart/form-data'
}
但它会生成以下错误:
Missing boundary in multipart/form-data POST
最佳答案
您需要使用 formdata.append(key,value)
函数将所有数据添加到 formdata
对象中。
然后您只需发送 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);
http请求中的对象数据
。
关于javascript - Vue-资源上传文件到PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51831431/