• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

javascript – ajax上传文件后台使用$_files接受为空

php 搞代码 3年前 (2022-01-22) 17次浏览 已收录 0个评论

javascript代码:

<html><head><meta charset="UTF-8"><style>img{   max-width:80%;   display:block;}</style></head><body><input id="img" type="file"><p id="text"></p><script>var up=function(o,success,x,file){   if(typeof success=='function')   file=o;   x=new XMLHttpRequest()   x.open('POST','http://127.0.0.1/up.php?r='+Math.random(),1)   x.onload=function(r){       r=x.responseText       if(success)//if r.pid and the twice parameter existing           return success(r)   }   x.send(file)}</script><script>img.onchange=function(){   if(!this.files||!this.files[0])       return alert('选取文件出错!')   var    imgfile=this.files[0]   if(imgfile.type.indexOf('image')!=0)       return alert('这不是一个图像或音频!')      up(imgfile,function(r){       text.innerHTML=r   })}</script></body></html>

后台代码:

<?php  header('Access-Control-Allow-Origin:*');  header("Content-Type:text/json;charset=utf-8");  echo json_encode($_FILES);   ?>

返回数据为[]

回复内容:

javascript代码:

<html><head><meta charset="UTF-8"><style>img{   max-width:80%;   display:block;}</style></head><body><input id="img" type="file"><p id="text"></p><script>var up=function(o,success,x,file){   if(typeof success=='function')   file=o;   x=new XMLHttpRequest()   x.open('POST','http://127.0.0.1/up.php?r='+Math.random(),1)   x.onload=function(r){       r=x.responseText       if(succes<p>4本文¥来源gao!%daima.com搞$代*!码$网9</p><pre>搞代gaodaima码

s)//if r.pid and the twice parameter existing return success(r) } x.send(file)}</script><script>img.onchange=function(){ if(!this.files||!this.files[0]) return alert('选取文件出错!') var imgfile=this.files[0] if(imgfile.type.indexOf('image')!=0) return alert('这不是一个图像或音频!') up(imgfile,function(r){ text.innerHTML=r })}</script></body></html>

后台代码:

 <?php  header('Access-Control-Allow-Origin:*');  header("Content-Type:text/json;charset=utf-8");  echo json_encode($_FILES);   ?>

返回数据为[]

异步上传文件,应使用FormData。例如:

var oMyForm = new FormData();oMyForm.append("file", file);x.send(oMyForm);

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:javascript – ajax上传文件后台使用$_files接受为空

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址