使用Ajax将图像传递到PHP
我试图将图像和标题字段值传递给PHP,我通常使用$\u FILES数组直接使用PHP处理文件上传,我不确定如何使用ajax创建/传递此数组到PHP。 我的表格: 在PHP中,我尝试以下方法:使用Ajax将图像传递到PHP,php,ajax,file-upload,Php,Ajax,File Upload,我试图将图像和标题字段值传递给PHP,我通常使用$\u FILES数组直接使用PHP处理文件上传,我不确定如何使用ajax创建/传递此数组到PHP。 我的表格: 在PHP中,我尝试以下方法: if (isset($_POST["edit"])) { $id = $_POST["edit"]; $imgName = $_FILES["file"]["imgName"]; } 您可以使用jQuery文件上传插件 $('#imgOne').change(function(){
if (isset($_POST["edit"])) {
$id = $_POST["edit"];
$imgName = $_FILES["file"]["imgName"];
}
您可以使用jQuery文件上传插件
$('#imgOne').change(function(){
$.ajaxFileUpload({
fileElementId: 'imgOne',
url: "../Controller/ProductController.php",
dataType: 'json',
success: function(response)
{
//Success Code Here
},
error: function()
{
//Error Code Here
}
});
});
我在我的一个项目中这样做了,下面的代码对我很有用。 请根据需要对代码进行必要的修改 我的表单按钮:
<form name="upload_img" enctype="multipart/form-data" id="upload_img">
<input type="file" style="display:none" id="upload-image" name="upload-image"></input>
<button id="upload_image" type="button">Save</button>
</form>
更多信息:您需要
多部分/表单数据
:jQuery对此进行了抽象,使其更具跨浏览器性(尽管不普遍支持):
input type="file" id="image_to_upload"
jQuery.noConflict();
formdata = new FormData();
jQuery("#image_to_upload").on("change", function() {
var file = this.files[0];
if (formdata) {
formdata.append("image", file);
jQuery.ajax({
url: "destination_ajax_file.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success:function(){}
});
}
});
print_r($_FILES);