gpt4 book ai didi

php - 我可以使用 jquery post 上传图片吗?

转载 作者:可可西里 更新时间:2023-11-01 13:23:15 25 4
gpt4 key购买 nike

我正在尝试使用 jquery 来提交我的表单,但它没有触发 functions.php 文件中的任何内容。我需要对多部分/表单数据做任何特别的事情吗?我错过了什么吗?

HTML:

<form id="process_image_form" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'], ENT_QUOTES); ?>" method="post" >
<input type="hidden" name="image_id1" id="image_id1" />
<table class="bordered-table">
<tbody>
<tr>
<td><input name="file" type="file" id="file"/></td>
</tr>
<tr>
<td><button type="submit" class="btn primary" data-loading-text="Uploading..." id="upload_profile_photo" name="upload_profile_photo">Upload</button></td>
</tr>
</tbody>
</table>
</form>

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");
});

函数.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 = '<div class="alert-message error">Your image file is too large. Reduce its size and try uploading again.</div>';
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 = '<div class="alert-message success">Your profile photo was uploaded successfully!</div>';
echo json_encode($res);
exit();
}
}
}
else {
$res->error = '<div class="alert-message error">Please select a photo to upload.</div>';
echo json_encode($res);
exit();
}
}

最佳答案

jquery 序列化方法不适用于文件输入。请看这里:How can I upload files asynchronously?


我对 jquery file upload 有很好的体验用于此目的的插件。

关于php - 我可以使用 jquery post 上传图片吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9042164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com