2013-03-19 34 views
4

我試圖從圖片庫(在WP7上)上傳圖片並將其保存在服務器上的一個文件夾中。將Windows Phone 7應用程序上傳到PHP

在服務器上,我使用PHP來使用POST方法接收文件。 PHP代碼爲:

<?php 
$uploads_dir = 'files/'; //Directory to save the file that comes from client application. 
if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) { 
    $tmp_name = $_FILES["file"]["tmp_name"]; 
    $name = $_FILES["file"]["name"]; 
    move_uploaded_file($tmp_name, "$uploads_dir/$name"); 
} 
?> 

我已經嘗試了一些方法,但他們都只是忽視了。 我已經使用Client.UploadFile方法在Windows窗體應用程序中完成了這項工作,但似乎無法在Windows Phone應用程序上使用它。

我認爲httpwebrequest可以幫助,對不對?

這是迄今爲止我的C#代碼:

public partial class SamplePage : PhoneApplicationPage 
    { 
     public SamplePage() 
     { 
      InitializeComponent(); 
     } 

     PhotoChooserTask selectphoto = null; 

     private void SampleBtn_Click(object sender, RoutedEventArgs e) 
     { 
      selectphoto = new PhotoChooserTask(); 
      selectphoto.Completed += new EventHandler<PhotoResult>(selectphoto_Completed); 
      selectphoto.Show(); 
     } 

     void selectphoto_Completed(object sender, PhotoResult e) 
     { 
      if (e.TaskResult == TaskResult.OK) 
      { 
       BinaryReader reader = new BinaryReader(e.ChosenPhoto); 
       image1.Source = new BitmapImage(new Uri(e.OriginalFileName)); 
       txtBX.Text = e.OriginalFileName; 
      } 
     } 
    } 

我的地方閱讀,圖像必須被轉換爲字節串,我不知道。 但是,請幫助我。

非常感謝。

回答

4

我將圖像轉換爲Base64(參見System.Convert),然後通過郵局匯款:

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://mydomain.cc/saveimage.php"); 
      request.Method = "POST"; 
      request.ContentType = "application/x-www-form-urlencoded"; 
      string postData = String.Format("image={0}", myBase64EncodedImage); 

      // Getting the request stream. 
      request.BeginGetRequestStream 
       (result => 
       { 
        // Sending the request. 
        using (var requestStream = request.EndGetRequestStream(result)) 
        { 
         using (StreamWriter writer = new StreamWriter(requestStream)) 
         { 
          writer.Write(postData); 
          writer.Flush(); 
         } 
        } 

        // Getting the response. 
        request.BeginGetResponse(responseResult => 
        { 
         var webResponse = request.EndGetResponse(responseResult); 
         using (var responseStream = webResponse.GetResponseStream()) 
         { 
          using (var streamReader = new StreamReader(responseStream)) 
          { 
           string srresult = streamReader.ReadToEnd(); 
          } 
         } 
        }, null); 
       }, null); 
     } 

saveimage.php應該是這樣的:

<? 
function base64_to_image($imageData, $outputfile) { 
    /* encode & write data (binary) */ 
    $ifp = fopen($outputfile, "wb"); 
    fwrite($ifp, base64_decode($imageData)); 
    fclose($ifp); 
    /* return output filename */ 
    return($outputfile); 
}  

if (isset($_POST['image'])) { 
    base64_to_jpeg($_POST['image'], "my_path_to_store_images.jpg"); 
} 
else 
    die("no image data found"); 
?> 

注:我沒有測試過代碼。可能存在拼寫錯誤或其他錯誤。這只是爲了說明如何使用POST傳輸圖像。

編輯爲repy您的意見:我沒有代碼在手編碼爲base64但這裏是你將如何在C#解碼base64編碼的圖像:

byte[] image = Convert.FromBase64String(str); 
+0

嘿,非常感謝! 這正是我所期待的。 但是,我無法將圖像轉換爲base64。 你能幫我解決一下嗎? 我會非常感謝你! :-) – 2013-03-20 13:09:04

+0

看到我的編輯在答案... – thomiel 2013-03-20 14:48:34

+0

你已經幫了我很多。 非常感謝。 但是,我仍然希望將它編碼爲base64。 :-) 謝謝,無論如何。 – 2013-03-21 00:59:55

相關問題