【发布时间】:2014-11-21 01:28:10
【问题描述】:
我正在尝试将文件上传到 php 我的 server.file 并通过多部分 /form-data 上传数据,在 php 服务器上接收到的文件和数据但在我的 php 服务器中返回 json 响应。请帮助我如何读取 json 响应在我的网页中,如果它的成功(代码 = 0)意味着它重定向另一个页面。php服务器对于android和网页都很常见。json响应看起来像{“code”:0,“message”:“success”}
<div style="height:0px;overflow:hidden">
<form id="myForm" action="http://192.168.2.4/digiid/api/addid"
method="post" enctype="multipart/form-data" runat="server">
<input type="file" name="file" id="file" onchange="showMyImage(this)" />
<input type="hidden" name="userid" value="<?php echo $_SESSION["userid"]?>">
<input type="hidden" id="inputfilename" name="filename" value="here">
</form>
</div>
<a class="button1" id="browseButton" onclick="" style="width:12%;height: 30px; text-decoration:none;"><font color="white" size="5px">Select ID</font></a>
<br/>
<div>
<img src='images/capture_picture_size.png' id='imgscreen' width='200' height='200'>
<br/>
<p id="filename" style="color: #ffffff; font-size: 20px" >
Title of the ID<br/></p>
<a class="button1"onclick="myFunction()" style= " width:12%;height: 30px; text-decoration:none;"><font color="white" size="5px">Save ID</font></a></form>
</div>
<script>
function myFunction() {
document.getElementById("myForm").submit();
}
</script>
<script>
browseButton.onclick=function chooseFile() {
document.getElementById("file").click();
};
function showMyImage(fileInput) {
var files = fileInput.files;
var file = files[0];
var imageType = /image.*/;
var img=document.getElementById("imgscreen");
var reader = new FileReader();
reader.onload = (function(aImg) {
return function(e) {
//x=e.target.result
img.src = e.target.result;
var extfilename=file.name;
document.getElementById("filename").innerHTML=extfilename.slice(0,-5) ;
document.getElementById("inputfilename").value=extfilename.slice(0,-5);
};
})(img);
reader.readAsDataURL(file);
}</script>
【问题讨论】:
-
json 响应是什么样的?
-
json 响应看起来像 {"code":0,"message":"success"}
-
您能重新格式化您的代码吗?为什么您尝试访问 $_POST['photo'] ?您的表单中没有这样的输入。使用 $_FILE 而不是 $_POST 访问文件上传
-
发布您的
php服务器端代码
标签: javascript php jquery html json