我可以使用jquerypost上传图片吗?

我正在尝试使用jquery提交我的表单,但它不会触发functions.php文件中的任何内容。 我需要对multipart / form-data做一些特别的事情吗? 我错过了什么吗?

HTML:

<form id="process_image_form" enctype="multipart/form-data" action="" method="post" >  

Jquery电话:

 $('#upload_profile_photo').click(function(e) { e.preventDefault(); $.post('gallery/functions.php', $("#process_image_form").serialize(), function(status) { if (status.st) { alert("Photo Uploaded"); } }, "json"); }); 

的functions.php:

 if (isset($_POST['upload_profile_photo'])) { if (isset($_FILES['file']) && $_FILES['file']['size'] > 0) { //handle file upload $size = filesize($_FILES['file']['tmp_name']); if ($size > $max_file_size * 1024 * 1024) { $res->error = '
Your image file is too large. Reduce its size and try uploading again.
'; echo json_encode($res); exit(); } if ($res->error == "") { //process image $res = uploadImage($_FILES['file']['tmp_name'], $user_id); if ($res->st) { unlink($_FILES['file']['tmp_name']); $res->msg = '
Your profile photo was uploaded successfully!
'; echo json_encode($res); exit(); } } } else { $res->error = '
Please select a photo to upload.
'; echo json_encode($res); exit(); } }

jquery serialize方法不适用于文件输入。 请看这里: 我如何异步上传文件?


为此,我对jquery文件上传插件有很好的经验。