2014-02-09 87 views
1

我正在開發一個將圖像上傳到服務器的移動android項目。我正在使用服務器上的PHP腳本幫助在Flash Builder中完成此工作。我的網頁由goDaddy託管,我已經完成了上傳文件的權限;我可以通過網頁上傳圖片。但是,當我嘗試從應用程序發送時,它不會上傳。有關這個問題的任何想法?我的代碼如下所示;Flash Builder將照片上傳到服務器

Flash Builder中:在服務器上

<?xml version="1.0" encoding="utf-8"?> 
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
     xmlns:s="library://ns.adobe.com/flex/spark" 
     title="Photo Upload"> 

    <fx:Script> 
     <![CDATA[ 
      private var urlRequest:URLRequest = new URLRequest("http://www.mywebsite.com/upload_file.php"); 
      private var file:File; 

      //take a new picture with the camera 


      //select a picture from the camera roll (gallery) 
      protected function uploadGallery_clickHandler(event:MouseEvent):void 
      { 
       if (CameraRoll.supportsBrowseForImage) 
       { 
        trace("camera roll is supported"); 
        var roll:CameraRoll = new CameraRoll(); 
        roll.browseForImage(); 
        roll.addEventListener(MediaEvent.SELECT,selectCompleteHandler); 
       } 
       else 
       { 
        trace("camera roll not supported"); 
        statusText.text = "Camera roll not supported on this device."; 
       } 
      } 

      //when the selection is complete upload it 
      protected function selectCompleteHandler(event:MediaEvent):void 
      { 
       trace("event.data.file.url; = "+event.data.file.url); 

       file = event.data.file; 
       file.addEventListener(Event.COMPLETE,uploadCompleteHandler); 
       file.addEventListener(Event.OPEN,openUploadHandler); 
       file.upload(urlRequest); 

      } 

      protected function uploadCompleteHandler(event:Event):void 
      { 
       trace("upload complete"); 
       statusText.text = "Photo Uploaded"; 
      } 

      protected function openUploadHandler(event:Event):void 
      { 
       trace("uploading"); 
       statusText.text = "Uploading..."; 
      } 
     ]]> 
    </fx:Script> 

    <s:VGroup x="21" y="23" width="200" height="200"> 
     <s:Label id="statusText" fontSize="24" text="Choose a Photo..."/> 
     <s:Button id="galleryPhotoButton" label="Upload from Gallery" 
        click="uploadGallery_clickHandler(event)"/> 
    </s:VGroup> 
</s:View> 

PHP腳本:

<?php 
$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$temp = explode(".", $_FILES["file"]["name"]); 
$extension = end($temp); 
if ((($_FILES["file"]["type"] == "image/gif") 
|| ($_FILES["file"]["type"] == "image/jpeg") 
|| ($_FILES["file"]["type"] == "image/jpg") 
|| ($_FILES["file"]["type"] == "image/pjpeg") 
|| ($_FILES["file"]["type"] == "image/x-png") 
|| ($_FILES["file"]["type"] == "image/png")) 
&& ($_FILES["file"]["size"] < 20000) 
&& in_array($extension, $allowedExts)) 
    { 
    if ($_FILES["file"]["error"] > 0) 
    { 
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>"; 
    } 
    else 
    { 
    echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
    echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
    echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 

    if (file_exists("upload/" . $_FILES["file"]["name"])) 
     { 
     echo $_FILES["file"]["name"] . " already exists. "; 
     } 
    else 
     { 
     move_uploaded_file($_FILES["file"]["tmp_name"], 
     "upload/" . $_FILES["file"]["name"]); 
     echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; 
     } 
    } 
    } 
else 
    { 
    echo "Invalid file"; 
    } 
?> 

回答

0

你得到任何錯誤或以上代碼的執行任何異常? 您也可以嘗試添加urlRequest方法,如下所示:

urlRequest.method = URLRequestMethod.POST; 
相關問題