0

这是我已经做过几次的事情,但是现在使用相同的方法将无法正常工作。这是一个简单的图像上传脚本,它发布到一些 PHP 进行处理,我希望 PHP 输出文件名以供 JS 读取。

这是我的 jQuery:

$(function(){
  $('#photoimg').change(function(){
    var formData = new FormData($('#img_form')[0]);
    var wHeight = $(window).height();
    // get height of browser
    var wWidth =  $(window).width();
    // get width of browser
    $('#loading').show();
    $.ajax({
      url: '../_process/calendar_upload.php?w='+ wWidth +'&h='+ wHeight +'',
      //send browser height and width to scale temp image to fit to screen
      type: 'POST',
      success: function (resp) {
        $('#loading').hide();
        //write scaled image to popup
        var fname = resp.imgname;
        $("#cal_imageuploader").html("<img src='../_img/event_images/'"+ fname +" '/>");
        //loads temp image into popup for cropping
        $('#cal_imageuploader').bPopup();
        //opens popup
        //trigger cropping on photo
      },
      data: formData,
      cache: false,
      contentType: false,
      processData: false
    });
  });
});

以及它被发布到的 PHP:

<?php
  $session_id = mysql_escape_string($_COOKIE['Foo']);
  // User session id
  $path = "../_img/event_images/";
  $tmp_path = "../_img/tmp";
  $wHeight = $_REQUEST['h'];
  $wWidth = $_REQUEST['w'];
  $valid_formats = array("jpg", "png", "gif", "bmp", "jpeg");
  if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
    $name = $_FILES['photoimg']['name'];
    $size = $_FILES['photoimg']['size'];
    if (strlen($name)) {
      list($txt, $ext) = explode(".", $name);
      if (in_array($ext, $valid_formats)) {
        if ($size < (3024 * 3024)) {
        // Image size max 3 MB
          $hash = "49esd34h";
          $randstring = rand(5, 10);
          $actual_image_name = $session_id."_".sha1($name . $hash . $randstring) . "." . $ext;
          $tmp = $_FILES['photoimg']['tmp_name'];
          //tmp file code goes here
          if (move_uploaded_file($tmp, $path . $actual_image_name)) {
            $resp['imgname'] = $actual_image_name;
            echo json_encode($resp);
          } else
            echo "failed";
        } else
          echo "Image too large...";
      } else
        echo "Invalid file format...";
    } else
      echo "Please select image..!";
    exit;
  }
?>

目前 varfname未定义,我想不出为什么。有任何想法吗?

4

2 回答 2

1

尝试通过 dataType : 'json'在ajax中指定

$.ajax({
      url: '../_process/calendar_upload.php?w='+ wWidth +'&h='+ wHeight +'',
      //send browser height and width to scale temp image to fit to screen
      type: 'POST',
      dataType : 'json',
      success: function (resp) {
        $('#loading').hide();
        //write scaled image to popup
        var fname = resp.imgname;
        $("#cal_imageuploader").html("<img src='../_img/event_images/'"+ fname +" '/>");
        //loads temp image into popup for cropping
        $('#cal_imageuploader').bPopup();
        //opens popup
        //trigger cropping on photo
      },
      data: formData,
      cache: false,
      contentType: false,
      processData: false
    });
于 2013-05-27T13:14:26.437 回答
0

我认为响应对象没有 imgname。

我猜 $actual_image_name 没有得到任何值,你能检查变量的值吗?

于 2013-05-27T13:03:08.773 回答