【发布时间】:2016-11-01 08:39:28
【问题描述】:
我有一个页面,我在其中使用输入类型 = 文件将图像上传到使用此代码的图像标签
$('#profile-image-upload').change( function(event) {
var path = URL.createObjectURL(event.target.files[0]);
$("#image").attr('src',URL.createObjectURL(event.target.files[0]));
这给了我一个像这样 blob:http://localhost:8080/467d02c9-0af0-448a-a239-69e8d4037dd1 的路径的 bolb 数据。我的图像在路径变量中保存的这个 blob 数据中
现在我还希望将此图像文件保存在我的服务器目录中,并使用 ajax 将其发送到我的服务器
var ajax;
if (window.XMLHttpRequest) // For modren Browsers
ajax=new XMLHttpRequest();
else
ajax=new ActiveXObject("Microsoft.XMLHTTP"); // For IE5 & IE8
ajax.open("POST","../UploadImg.php",true);
ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var data= "path="+path+"&type="+user+"&id="+id+"&extent="+extent;
//path = blob data
//user = user name
//id = user id
//extent = extension of image
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && ajax.status==200){
alert(ajax.responseText);
}
}
ajax.send(data);
});
我的服务器端编码是
$img_ff = $_POST["path"]; // blob path
$user = $_POST["type"];
$id = $_POST["id"];
$extension = $_POST["extent"];
$dst_img= $user.$id.".".$extention; // for example user17.png
$dst_path= "localhost:8080/live4others/images/";
$dst_cpl = $dst_path . $dst_img;
if(move_uploaded_file($dst_cpl, $img_ff)){
echo "yes";
}else{
echo "error";
}
if( file_put_contents( $img_ff, $dst_cpl)){
echo "yes";
}else{
echo "error";
}
这两种方法都不起作用。请告诉我我在哪里做错了。他们给出错误并说给定的路径无效。
【问题讨论】:
标签: javascript php image blob