2013-12-12 51 views
0

我想通过提交表单上传图像。以下是我的代码片段:图像不通过ajax上传

HTML:

<form method="POST" id="statusform" action="insertstatus.php" enctype="multipart/form-data"> 
<textarea name="statusText" onclick="javascript:this.value='';" class="retroText" style="width:600px;height:100px;font-family:lucida sans unicode,lucida grande,sans-serif;resize:none;padding:5px;">Post your crap here ...</textarea> 
<input type="file" name="statusPhoto" accept="image/gif, image/jpeg, image/x-ms-bmp, image/x-png" size="26" /> 
</form> 

的jQuery:

$("#statusform").submit(function() { 

     $.post($("#statusform").attr("action"), $("#statusform").serialize(), function(data){   
      alert(data); 
     }); 


     //Important. Stop the normal POST 
     return false; 
    }); 

PHP:

if(isset($_FILES['statusPhoto']) && $_FILES['statusPhoto']['size'] > 0) 
{ 
     <Image Upload Code> 
} 
else 
echo "Photo not submitted"; 

消息换货政...从阿贾克斯是:未提交照片。 请帮助.. !!

+0

你必须使用一个FORMDATA对象上传与阿贾克斯 – Musa

回答

1

你正在上传,而不是图像,你上传它的名字。

$("#statusform").serialize()返回一个字符串,图像是一个blob。例如,尝试使用一些jQuery plugins

+0

一个文件,我只是测试这是否$ _FILES [ 'statusPhoto']被设置或不是..但是这件事本身在这里失败.. – cipherux

+0

好的。我明白那个。但是,如果我不想使用插件,可能的解决方案是什么。 ? – cipherux

+0

'$ _FILES'将永远为你的'$ .post'清空,因为你不发送文件内容,只发送它的名字。如果您不想重新加载页面,请尝试将表单提交给iframe。 – Pinal