苦苦挣扎JSON文件上传

时间:2015-09-20 13:23:43

标签: php json file xmlhttprequest

对所有选项感到困惑 - 准备粉碎电脑!!如果有人可以帮助我,请。

我有一个HTML5 / JS APP(在XDK中),它可以将一些数据发送到PHP服务器,可以获取数据但是仍然在使用图像文件。

发送的JS如下;



   <script>
            function sendDataJS()
             {
                 var xmlhttp = new XHRObject();                
                 xmlhttp.onreadystatechange=function()
                 {
                     if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {  
                    document.getElementById('ResponseDiv').innerHTML = "Entry Posted";                       
                    }
                 }                               
                 var postdata = '{';
                 postdata += '"SBP_Category": "' + document.getElementById('Category').value + '"';
                 postdata += ',"SBP_Details": "' + document.getElementById('Details').value + '"';
                 postdata += ',"upload_file": "' + document.getElementById('mypic').files[0] + '"';
                 
                 postdata += '}';
                     
                 xmlhttp.open("POST","http://strxxx.co.uk/SBPostPost.php", false);
                 xmlhttp.setRequestHeader("Content-type","application/json");
                 
                xmlhttp.send(postdata);
             }
        </script>
&#13;
&#13;
&#13;

发送内容的控制台日志如下;

&#13;
&#13;
Request URL:http://strxxx.co.uk/SBPostPost.php
Request Method:POST
Status Code:200 OK
Request Headers
Provisional headers are shown
Content-type:application/json
Origin:http://127.0.0.1:58889
Referer:http://127.0.0.1:58889/http-services/emulator-webserver/ripple/userapp/x/C/Users/Phils/AppData/Local/XDK/xdk-scratchdir/e14eb410-7cf2-4bed-ba25-515e6df98e8c/platforms/ios/www/index.html
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4
X-DevTools-Emulate-Network-Conditions-Client-Id:BFC389F1-2AF4-4347-8AC1-F9144DC2DA25
Request Payloadview source
{SBP_Category:Select, SBP_Details:xxxxx Detail xxxx, upload_file:[object File]}
SBP_Category: "Select"
SBP_Details: "xxxxx Detail xxxx"
upload_file: "[object File]"
Response Headers
Content-Encoding:gzip
Content-Length:251
Content-Type:text/html
Date:Sun, 20 Sep 2015 13:11:33 GMT
Server:Microsoft-IIS/8.5
Vary:Accept-Encoding
&#13;
&#13;
&#13;

.....显示正在发送的对象....

我拥有的PHP;

&#13;
&#13;
 	$headers = getallheaders();

	if ($headers["Content-Type"] == "application/json") {
 	 	$jsondata = file_get_contents("php://input");    
		$data = json_decode($jsondata,TRUE); 	  	  				
  		echo ($data['SBP_Category']."/".$data['SBP_Details']);
  		echo ($data['upload_file']);  	
  	}
  	
	if (isset($_FILES['upload_file'])) {
    	if(move_uploaded_file($_FILES['upload_file']['tmp_name'], "temp/" . $_FILES['upload_file']['name'])){
        echo $_FILES['upload_file']['name']. " OK";
    } else {
        echo $_FILES['upload_file']['name']. " KO";
    }
    exit;
} else {
    echo "No files uploaded ...";
}
&#13;
&#13;
&#13;

回顾的是类别和数据中的数据。详细信息字段和$ data [&#39; upload_file&#39;]的回显显示&#39;对象&#39; (所以我认为它知道它是一个文件 - 或者是它?

以下&#34;如果是ISSET&#34;然而只是回声“没有文件上传&#39; ??

有人可以告诉我如何获取此文件并保存)

非常感谢。

0 个答案:

没有答案