如何解决使用xml请求将文件上传到数据库
这就是我想要做的:
-
我没有表格。我只有几个输入字段和导致一些js代码的按钮。
-
使用js完成验证后-出现.submit-form按钮。
-
如果按下-它将从所有输入字段中收集值。
-
然后我发送一个xml请求。我使用POST方法将变量传递到PHP脚本中。
-
我还想将图像作为文件传递,然后将所有数据与图像一起保存到我的数据库中。
-
如果我尝试使用
$_POST
获取所有值-效果很好。但是,如果我尝试使用此图像:$image = file_get_contents($_FILES[$image_post]["temp_name"]);
失败,并说:
file_get_contents(): Filename cannot be empty;
$('.submit-form').click(function() {
const xhr = new XMLHttpRequest();
xhr.onload = function() {
const serverResponse = document.getElementById("data-comments");
serverResponse.innerHTML = this.responseText;
$(".comments").css("display","block");
};
xhr.open("POST","js/admin/post_product.php");
xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//finding the child elements for each of the shops
$(".shops").children().each(function (){
//Source
adress = "#"+this.id+" "+"."+"SourceClass";
item = $(adress);
id = "#"+item.attr("id");
value = $(id).val();
var source = ""
source = source.concat("&Source"+id.slice(-1)+"="+value);
//Link
//In this part I am finding the id of the element
adress = "#"+this.id+" "+"."+"LinkClass";
item = $(adress);
id = "#"+item.attr("id");
value = $(id).val();
var link = "";
//creating the part of the ajaxRequest
link = link.concat("&Link"+id.slice(-1)+"="+value);
//Price
//In this part I am finding the id of the element
adress = "#"+this.id+" "+"."+"PriceClass";
item = $(adress);
id = "#"+item.attr("id");
value = $(id).val();
var price = "";
//creating the part of the ajaxRequest
price = price.concat("&Price"+id.slice(-1)+"="+value);
//Image
//In this part I am finding the id of the element
adress = "#"+this.id+" "+"."+"ImageClass";
item = $(adress);
id = "#"+item.attr("id");
value = $(id).val();
var image = "";
//creating the part of the ajaxRequest
image = image.concat("&Price"+id.slice(-1)+"="+value);
ajaxRequest = ajaxRequest.concat(source,link,price,image);
});
xhr.send(ajaxRequest);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
此外,该数据库是phpmyadmin,并且正在使用blob数据类型作为图像。 有人可以推荐我如何将图像正确添加到$ _FILES吗?因为我相信这就是错误所在。另外,我所有的代码都是使用这种方法制作的,我真的不想更改仅用于图片上传的所有内容。
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。