我需要你的帮助。我有一个消息系统,用户可以在其中附加文件。但是我的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 :(得分:0)
尝试dataType:'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();