我正在尝试使用ajax post方法将图像文件传递给php文件。脚本发送对象,但我的解析器没有得到$ _FILES [“avatar”] [“name”]和tmp_name。有没有办法像表单那样通过AJAX发送文件?
我的HTML:
<input type="file" id="avatar_file" name="avatar" />
<input type="file" id="title_file" name="title" />
<input name="myBtn" type="submit" id="save_changer" value="Submit Data" onclick="subForm();">
我的Javascript:
function subForm(){
var hr = new XMLHttpRequest();
var url = "php_parsers/photo_system.php";
var fn = document.getElementById("avatar_file").files[0];
var formData = new FormData();
formData.append("avatar", fn);
hr.open("POST", url, true);
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
hr.onreadystatechange = function() {
if(hr.readyState == 4 && hr.status == 200) {
var return_data = hr.responseText;
alert(fn);
}
}
hr.send(formData);
}
我的PHP Parser(php_parsers / photo_system.php):
if (isset($_FILES["avatar"]["name"]) && $_FILES["avatar"]["tmp_name"] != ""){
$fileName = $_FILES["avatar"]["name"];
$fileTmpLoc = $_FILES["avatar"]["tmp_name"];
$fileType = $_FILES["avatar"]["type"];
$fileSize = $_FILES["avatar"]["size"];
$fileErrorMsg = $_FILES["avatar"]["error"];
答案 0 :(得分:0)
删除此行
val intent = getPackageManager().
getLaunchIntentForPackage("my.package.name")
从您的代码中重试