douzhanlai4671 2014-06-11 15:40
浏览 45
已采纳

WinJS.xhr blob图像不在php服务器中显示?

Uploading an Image from win 8.1 tablet using winRT, winJS, javaScript & html 5.

I generating a blob from an image in pictures and sending it to server via winJS.xhr on RESTful api I have a function that captures the post and saves it to location on the linux server.

Problem is that image is empty or unreadable. The problem is in the php, i tested different option, nothing makes the img readable?

How to get the img?

winRT code:

 function uploadImg(){
 var url2="http://serverurl/sr/uploadpicture";

 var picturesLibrary = Windows.Storage.KnownFolders.picturesLibrary;  
 picturesLibrary.getFileAsync("test.bmp").then(

 function completeFile(file) {
      return file.openAsync(Windows.Storage.FileAccessMode.readWrite);
 }).then(
     function completeStream(stream) {
     // Do processing.
     var blob = MSApp.createBlobFromRandomAccessStream("image/bmp", stream);
     //document.getElementById('imgCapture').src=blob;

     var fd = new FormData();
     fd.append('test', 'lalalala');
     fd.append('data', blob);
     return WinJS.xhr({ type: "POST", url: url2, data: fd });
 }).then(
     function (request) {
         document.getElementById('txteserver').value = "uploaded file:"+request.response;
     },
     function (error) {
        document.getElementById('txteserver').value= "error uploading file";
     });
 }

Php server:

   /**
     *  
     *
     * @url POST /sr/uploadpicture
     */
    public function uploadpicture()
    {
    //  header("Content-Type: image/bmp");
        echo "test";
        echo $_POST['test'];

       $data = $_POST['data'];
       echo $data;
       echo 'isset';
       echo isset($_FILES['data']);

       if($_FILES['data']['error'] == 0){
            // success - move uploaded file and process stuff here
        echo 'success';
        }else{
            // 'there was an error uploading file' stuff here....  
            echo 'error uploading file'; 
        }
       echo var_dump($_FILES) ;

        if (($data)=="") {echo 'empty image ';}
        else { echo 'Testing uploading picture ';};
            $file = "test.bmp";
            $img=base64_decode($_FILES['data']['name']);
            $img2=base64_decode($_POST['data']);
           file_put_contents($file,img2);
}

echo var_dump($_FILES) result:

  {  ["data"]=>  array(5) {    ["name"]=>    string(4) "blob"    ["type"]=>    string(9) "image/bmp"    ["tmp_name"]=>    string(14) "/tmp/phpTg4t5M"    ["error"]=>    int(0)    ["size"]=>    int(254970)  }}

Thanks

  • 写回答

1条回答 默认 最新

  • douzhou7124 2014-06-13 14:59
    关注

    Working. Here's the answer:

    public function uploadpicture(){
    
    if($_FILES['data']['error'] == 0){
                // success - move uploaded file and process stuff here
            echo 'success';
    
             echo var_dump($_FILES) ;
             move_uploaded_file($_FILES["data"]["tmp_name"],"....server location".$_FILES["data"]["name"] );
    
            }else{
                // 'there was an error uploading file' stuff here....  
                echo 'error uploading file'; 
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决