繁体   English   中英

将图像上传到钛合金服务器中时无法读取php文件

[英]Can't read php file when upload image to server in titanium

我试图从图库中获取图像,然后使用钛中的Web服务将所选图像上传到服务器。

我用下面的代码。 但是正在收到调试错误:HTTP错误并且它还会显示警告框,例如“连接期间发生错误”

这段代码在我的开发服务器上可以正常工作,但是在我的客户端服务器上不能工作。 什么原因 ? 为什么我的代码在我的客户端服务器上不起作用?

从android设备上载文件时,文件上传工作正常。但是从iphone设备上载文件时,文件上传不起作用。能否给我一个解决此问题的想法?

为什么在我的控制台窗口上出现此错误。

 function AUF_ADD_File_FolderData () { 
  Titanium.Media.openPhotoGallery({
  success:function(event) {
      var request = Ti.Network.createHTTPClient({ 
               onload : function(e) {
        Ti.API.info(this.responseText);
        Ti.API.info("image pathe"+" "+event.media);
   if(this.responseText == "Successfully file is created"){
             var managefolders =Alloy.createController('manage_folders').getView();
       managefolders.open();  
         }
         else{
             alert(this.responseText); 
         }
    }, 
              onerror: function(e){ 
                  Ti.API.debug(e.error); 
                  alert("There was an error during the connection"); 
              }, 
              timeout:20000, 
                 });    
                   var uploadabc = event.media.imageAsResized(400 , 400);
                       request.open("POST",url+"client/manager/at_manager_create_common_file.php"); 

                 var params = ({"manager_id": manager_id,"file": uploadabc,}); 
               // var params = ({"manager_id": manager_id,"file": event.media,});   
              request.send(params); 

},

    cancel:function() {
        // called when user cancels taking a picture
    },
    error:function(error) {
        // called when there's an error
        var a = Titanium.UI.createAlertDialog({title:'Camera'});
        if (error.code == Titanium.Media.NO_CAMERA) {
            a.setMessage('Please run this test on device');
        } else {
            a.setMessage('Unexpected error: ' + error.code);
        }
        a.show();
    },
    saveToPhotoGallery:false,
    // allowEditing and mediaTypes are iOS-only settings
    allowEditing:true,
    mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
});
}

编辑:

这是php文件:

    <?php  

$request            = base64_decode($_POST['jsondata']);

$data               = json_decode($request,true);
$manager_id         = $data['manager_id'];
$file_name          = $data['file_name'];
$source             = base64_decode($data['source']);

include "db_connect.php";
// connecting to db
$db = new DB_CONNECT();

$result     = mysql_query("SELECT * from at_common_files WHERE user_id = '$manager_id'  and file_name = '$file_name'");
$no_of_rows = mysql_num_rows($result);
if ($no_of_rows > 0) {
    $response='{"Error":"1","Message":"Filename already existed"}';
    echo $response;
} else {
    $upload_dir = 'common_files/'.$manager_id."_".$file_name;

    file_put_contents($upload_dir,$source);
    $qugery     = mysql_query("insert into at_common_files (user_id,file_name) values ($manager_id, '$file_name') ");
    $response   = '{"Error":"0","Message":"Successfully file is created"}';
    echo $response;
}

?>

编辑:

由于正在出现以下错误:

:[DEBUG] HTTP错误 :[INFO] IN错误{“类型”:“错误”,“源”:{“缓存”:false},“代码”:404,“错误”:“ HTTP错误”,“成功“:假}

如果我调用了相同的URL并单独传递了一个manager_id,则结果很好。如果我传递了manager_id和文件,则这一次仅是Http错误。 我找不到确切的问题。因为相同的钛代码和php代码(开发服务器)工作正常,并且图像正在上传到开发服务器文件夹。 但是我已经将相同的php文件移到我的客户端server.now上不能用。 同样的Web服务URL在浏览器和android中也可以正常工作。它不仅在iphone中不工作,所以我找不到问题出在哪里? 你能给我一个解决方案吗?

编辑:请参考以下链接:

http://developer.appcelerator.com/question/174462/image-not-uploading-from-iphone#comment-224007

我遇到了完全相同的问题。请给我一个解决方案。

我发现了很多类似的问题(例如,“被动”连接“ <appname>”对受保护服务的访问被拒绝 )。

答案总是:
“这个错误就是所谓的“红鲱鱼”。这是一个令人误解的线索。HID并不是影响您应用程序的真正错误。应该有其他消息可能表明正在发生的情况。” 因此,请查看是否还有其他错误说明可以描述您的问题。

例如,尝试转义您在sql语句中使用的文件名:

$file_name = mysql_real_escape_string($data['file_name']);

确保您的设备已连接到互联网,然后尝试如下操作:

钛:

function AUF_ADD_File_FolderData () { 
    Titanium.Media.openPhotoGallery({
        success:function(event) {

            var xhr = Titanium.Network.createHTTPClient();

            xhr.onerror = function(e){
                Ti.API.info('IN ERROR ' + JSON.stringify(e));
                alert('Sorry, we could not upload your photo! Please try again.');
            };

            xhr.onload = function(){
                Ti.API.info(this.responseText);
                Ti.API.info("image pathe"+" "+event.media);
                if(this.responseText == "Successfully file is created"){
                    var managefolders =Alloy.createController('manage_folders').getView();
                    managefolders.open();  
                }else{
                    alert(this.responseText); 
                }            
            };

            xhr.open('POST', url+"client/manager/at_manager_create_common_file.php");

            xhr.send({
                media: event.media,
                manager_id: manager_id
            });     

        },
        cancel:function() {
            // called when user cancels taking a picture
        },
        error:function(error) {
            // called when there's an error
            var a = Titanium.UI.createAlertDialog({title:'Camera'});
            if (error.code == Titanium.Media.NO_CAMERA) {
                a.setMessage('Please run this test on device');
            } else {
                a.setMessage('Unexpected error: ' + error.code);
            }
            a.show();
        },
        saveToPhotoGallery:false,
        // allowEditing and mediaTypes are iOS-only settings
        allowEditing:true,
        mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]*/
    });
}

PHP:

<?php
    //this function returns a random 5-char filename with the jpg extension
    function randomFileName()
    {
       $length = 5;
       $characters = 'abcdefghijklmnopqrstuvwxyz';
       $string = '';    
       for ($p = 0; $p < $length; $p++) {
          $string .= $characters[mt_rand(0, strlen($characters))];
       }
       return $string . '.jpg';
    }

    //create the random filename string and uploads target variables
    $randomString = randomFileName();
    $target = 'common_files/'.$randomString;  


    if(move_uploaded_file($_FILES['media']['tmp_name'], $target))
    {
         echo "success";
    }
    else
    {   
         echo "moving to target failed"; 
    }
?>

有关更多信息,请检查此链接: http : //code.tutsplus.com/tutorials/titanium-mobile-build-an-image-uploader--mobile-8860

如果这样工作,您将不得不再次添加逻辑(调整大小和manager_id)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM