2017-01-19 27 views
0

我需要你的幫助。我有一個消息系統,用戶可以附加文件。但是我的ajax請求一直返回0,而不是顯示上傳文件的預覽。爲什麼我的ajax響應返回0而不是文件附件預覽?

這裏是我的ajax

$(document).ready(function() 

{ 
    jQuery('#message_attachment').on('change', function() { 

     var fromID = $.cookie('_user_directory_id'); 
     var toID = $.cookie('friends_username'); 

     close_urlfetched_message(); 

     $("#attachmentform").ajaxForm({ 

      beforeSubmit: function() 
      { 
       $('#attached_file').html('<img src="'+site_url+'img/loader.gif" alt="Uploading...." title=""/>'); 
      }, 
      target: '#attached_file', 
      url: site_url+'includes/wall.functions.php?type=22&from=' + fromID + '&to=' + toID, 
      success: function(response) 
      { 
       $('#attached_file').html(''); 

       if (response != "") 
       { 
        $('#attached_file').fadeIn(100).html(response); 
       } 
       else 
       { 
        $('#attached_file').fadeIn(100).html('<div id="message_info">Sorry, file attachment was unsuccessful.<br><br>Please try again or contact this site admin to report this error message if the problem persist. Thanks...</font></div>'); 
       } 
      } 
     }).submit(); 
    }); 

,這裏是我的PHP函數

else if($rtype == 22) // upload file in message 
{ 
    $path = "../views/media/pictures/uploads/message_attachment/"; 

    if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") 
    { 
     $name = $_FILES['attachedFile']['name']; 
     $size = $_FILES['attachedFile']['size']; 

     $allowedExtensions = unserialize (VALID_FILE_FORMAT_MESG); 

     foreach ($_FILES as $file) 
     { 
      if ($file['tmp_name'] > '' && strlen($name)) 
      { 
       if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) 
       { 
        echo '<div id="message_info">'.$txt_var_185.'</div>'; 
       } 
       else 
       { 
        if($size<(FILE_SIZE_FOR_MESG*FILE_SIZE_FOR_MESG)) 
        { 
         $ext = pathinfo($name, PATHINFO_EXTENSION); 

         $actual_image_name = time().rand(1234,9876).substr(str_replace(" ", "_", $txt), 5).".".$ext; 

         if(move_uploaded_file($_FILES['attachedFile']['tmp_name'], $path . $actual_image_name)) 
         { 
          mysqli_query($DBConnection, "insert into `messages_attachments_temp` values('', 
                        ".mysqli_real_escape_string($DBConnection, strip_tags($_GET["from"])).", 
                        ".mysqli_real_escape_string($DBConnection, strip_tags($_GET["to"])).", 
                        '".mysqli_real_escape_string($DBConnection, strip_tags($actual_image_name)."")."', 
                        '".mysqli_real_escape_string($DBConnection, strip_tags(date('d-m-Y')))."', 
                        '', 
                        '', 
                        '', 
                        '', 
                        '' 
                        )"); 

          ######## show all images 
          $checkfor_attachments = mysqli_query($DBConnection, "select * from `messages_attachments_temp` 
                   where 
                   `from` = ".mysqli_real_escape_string($DBConnection, $LoggedID)." and 
                   `to` = ".mysqli_real_escape_string($DBConnection, strip_tags($_COOKIE["friends_username"]))." AND file !='' 
                   order by `id` asc"); 

          if(mysqli_num_rows($checkfor_attachments) > 0) 
          { 
           while($getall_attachments = mysqli_fetch_array($checkfor_attachments)) 
           { 
            $file = $getall_attachments["file"]; 
            $id = $getall_attachments["id"]; 

            $ext = pathinfo($file, PATHINFO_EXTENSION); 

            $rembutton = '<a href="javascript:;" class="temp_img_remve_small" onclick="remove_temp_message_image('.$id.');">x</a>'; 

            if($ext == "gif" || $ext == "GIF" || $ext == "JPEG" || $ext == "jpeg" || $ext == "jpg" || $ext == "JPG" || $ext == "png" || $ext == "PNG") 
            { 
             echo '<span class="img" id="mesgfile'.$id.'">'.$rembutton.'<img src="'.BASE_URL.'views/media/pictures/uploads/message_attachment/'.$file.'" width="73" height="60"></span>'; 
            } 
            else if($ext == "doc" || $ext == "docx" || $ext == "rtf") 
            { 
             echo "<span class='img' id='mesgfile".$id."'>".$rembutton."<a href='".BASE_URL."views/media/pictures/uploads/message_attachment/".$file."' target='_blank'><img src='".BASE_URL."img/doc.png' width='75' height='60'></a></span>"; 
            } 
            else if($ext == "pdf") 
            { 
             echo "<span class='img' id='mesgfile".$id."'>".$rembutton."<a href='".BASE_URL."views/media/pictures/uploads/message_attachment/".$file."' target='_blank'><img src='".BASE_URL."img/pdf.png' width='75' height='60'></a></span>"; 
            } 
            else if($ext == "txt") 
            { 
             echo "<span class='img' id='mesgfile".$id."'>".$rembutton."<a href='".BASE_URL."views/media/pictures/uploads/message_attachment/".$file."' target='_blank'><img src='".BASE_URL."img/txt.png' width='75' height='60'></a></span>"; 
            } 
           } 
          } 
         } 
         else 
         { 
          echo "<div id='message_info'>".$txt_var_121."</div>"; 
         } 
        } 
        else 
        { 
         echo "<div id='message_info'>".$txt_img_up_size."</div>"; 
        } 
       } 
      } 
      else 
      { 
       echo "<div id='message_info'>You just canceled your attachment.</div>"; 
      } 
     } 
    } 
} 

回答

0

嘗試數據類型: 'HTML'

$("#attachmentform").ajaxForm({ 

     beforeSubmit: function() 
     { 
      $('#attached_file').html('<img src="'+site_url+'img/loader.gif" alt="Uploading...." title=""/>'); 
     }, 
     target: '#attached_file', 
     dataType : 'html', 
     url: site_url+'includes/wall.functions.php?type=22&from=' + fromID + '&to=' + toID, 
     success: function(response) 
     { 
      $('#attached_file').html(''); 

      if (response != "") 
      { 
       $('#attached_file').fadeIn(100).html(response); 
      } 
      else 
      { 
       $('#attached_file').fadeIn(100).html('<div id="message_info">Sorry, file attachment was unsuccessful.<br><br>Please try again or contact this site admin to report this error message if the problem persist. Thanks...</font></div>'); 
      } 
     } 
    }).submit(); 
+0

是它的工作!非常感謝。你搖滾男人! –

+0

謝謝:),請接受/ upvote我的回答@RomeoHennessy – mith

相關問題