簡體   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