javascript - 如何使用 jQuery 将存储在变量中的图像文件发送到 php
问题描述
我想使用 jQuery 将图像文件发送到 PHP 文件,首先将图像存储在变量中,然后将其发送以进行处理。php 代码输出如下错误:
未定义索引:第 2 行 C:\xampp\htdocs\edash\admin\checks\a_addQuest.php 中的 img
这是html代码。
<input type="file" name="img" id="img">
这是我的js代码。
$("#submitQuest").click(function() {
var dataString;
var img = $("#img")[0].files[0];
dataString = "img="+img;
$.ajax({
type : "POST",
url : "../admin/checks/a_addQuest.php",
data : dataString,
success: function (result) {
$("#output").html(result).fadeIn("slow", function () {
$("#output").fadeOut(3000);
});
}
});
return false;
});
这是php文件。
<?php
$img = $_FILES['img']['name'];
if($img){
echo "working";
}
?>
解决方案
$("#submitQuest").click(function() {
var data = new FormData();
data.append('file', $('#img')[0].files[0]);
$.ajax({
type : "POST",
url : "../admin/checks/a_addQuest.php",
data : data,
cache: false,
contentType: false,
processData: false,
success: function (result) {
$("#output").html(result).fadeIn("slow", function () {
$("#output").fadeOut(3000);
});
}
});
return false;
});
推荐阅读
- flutter - 按钮名称未显示在应用栏中,而标题谷歌地图可见
- d3.js - 如何按年份累积 d3.rollup 数据
- python-3.x - SQL Alchemy 枚举验证与验证函数冲突
- c# - 如何在 ZOHO CRM 中制作公式字段?
- sql - SQL Select FOR XML PATH - 将排序和连接字符串与递增计数器组合
- android - 在 Square.Retrofit 上,如何设置 CallAdapter.Factory 以便 Retrofit 使用我的 CallAdapter
>> - python - 从一个不规则纬度网格重新网格化到另一个不规则纬度网格
- python - python文件反编译加密文件
- android - 当ChannelResult为channelResult = Value(失败)时如何重试kotlin通道trySend
- powerbi - PowerBI - 根据最近的日期选择度量的最后一个非空白值