Ajax帖子在服务器上有空值

Ajax帖子在服务器上有空值

问题描述:

I'm trying to save an image file out of a base64 string received from the client side.

So I have this ajax post:

 $.ajax({type: "POST", url: "upload_post.php", data: postData, dataType: "text", success: function(result){
        alert("post result: " + result + " - data:" + postData);
        location.reload();
    }});

Here is an example of postData (which I know contains data):

{"ship_id":"407","base64_upload":"ABCSFSAFGDGFA....."}

Now here is a my php code that handles this post:

$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file. '.$file.'';

The problem is the $_POST variables are always empty. why is that? json related? location.reload() related? and how do I fix it?

EDIT

I have got these variables posted with actual data by doing JSON.parse(postData) on the ajax data. Now my problem is I still can't save the image file. any help?

我正在尝试将图像文件保存在从客户端收到的base64字符串之外。 p>

所以我有这个ajax帖子: p>

  $ .ajax({type:“POST”,url:“upload_post.php”,data:postData  ,dataType:“text”,success:function(result){
 alert(“post result:”+ result +“ -  data:”+ postData); 
 location.reload(); 
}}); \  n  code>  pre> 
 
 

以下是 postData code>(我知道包含数据)的示例: p>

  {“ship_id”:“407”,“base64_upload”:“ABCSFSAFGDGFA .....”} 
  code>  pre> 
 
 

现在这是我的PHP代码处理这个 发布: p>

  $ id = $ _POST ['ship_id']; 
 $ img = $ _POST ['base64_upload']; 
define('UPLOAD_DIR','news。  site.com/docs/'.$id.'/');
$img = str_replace('data:image / png; base64,','',$ img); 
 $ img = str_replace(''  ,'+',$ img); 
 $ data = base64_decode($ img); 
 $ file = UPLOAD_DIR。  uniqid()。  '.png'; 
 $ success = file_put_contents($ file,$ data); 
print $ success?  $ file:'无法保存文件。  '。$ file。''; 
  code>  pre> 
 
 

问题是$ _POST变量总是为空。 这是为什么? json有关吗? location.reload()有关吗? 以及如何修复它? p>

编辑 strong> p>

通过执行 ajax数据上的JSON.parse(postData) code>。 现在我的问题是我仍然无法保存图像文件。 有什么帮助吗? p> div>

Once again, I'm answering my own question. The problem was I have define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/'); and then $file = UPLOAD_DIR . uniqid() . '.png'; and this UPLOAD_DIR directory doesn't really exists. So I added a check if it exists and then created the directory before creating the file:

$id = $_POST['ship_id'];
$img = $_POST['base64_upload'];
define('UPLOAD_DIR', 'news.site.com/docs/'.$id.'/');
$file2 = UPLOAD_DIR;
if(!file_exists($file2)){
    mkdir($file2, 0777, true);
    $img = str_replace('data:image/png;base64,', '', $img);
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img);
    $file = UPLOAD_DIR . uniqid() . '.png';
    $success = file_put_contents($file, $data);
    print $success ? $file : 'Unable to save the file. '.$file.'';
}

This is what I do for jQuery Post. I hope it helps you

$.post("upload_post.php",
{
    ship_id: "407",
    base64_upload: "ABCSFSAFGDGFA....."
},
function(data, status){
    alert("post result: " + status+ " - data:" + data);
    location.reload();
});

You fetch the data in php exactly the same.