0

使用的HttpRequest我有一個二進制流,從Windows Phone中的PhotoChooser任務需要照片流。 我想上傳用戶選擇到我的Web服務器上的圖片。Windows Phone中

如何複製照片流HttpRequest中流?

到目前爲止,我有這樣的事情

BinaryStream BStream = new BinaryStream(StreamFromPhotoChooser); 
HttpRequest request = new HttpRequest() 

我知道如何爲HttpRequest中設置的屬性,以便它上傳到正確的位置。 我唯一的問題是ACTUAL上傳的圖片。

回答

0

你可以寫或複製您的二進制流通過獲取請求流和編寫流​​它來請求流。 這是某種形式的代碼,你會發現對Web請求有用的HttpWebRequest和在HttpRequest的流你的問題CAPY照片流中GetRequestStreamCallback完成()

private void UploadClick() 
{ 
     HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("your URL of server"); 
     myRequest.Method = "POST"; 
     myRequest.ContentType = "application/x-www-form-urlencoded"; 

     myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest); 
} 

private void GetRequestStreamCallback(IAsyncResult asynchronousResult) 
{ 
     try 
     { 
      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 
      System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult); 


      //your binary stream to upload 
      BinaryStream BStream = new BinaryStream(StreamFromPhotoChooser); 
      //copy your data to request stream 
      BStream.CopyTo(postStream); 

      //OR 

      byte[] byteArray = //get bytes of choosen picture 
      // Write to the request stream. 
      postStream.Write(byteArray, 0, postData.Length); 
      postStream.Close(); 

      //Start the asynchronous operation to get the response 
      request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request); 
     } 
     catch (WebException e) 
     { 

     } 
} 

private void GetResponseCallback(IAsyncResult asynchronousResult) 
{ 
     try 
     { 
      HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 
      HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 

      //to read server responce 
      Stream streamResponse = response.GetResponseStream(); 
      string responseString = new StreamReader(streamResponse).ReadToEnd(); 
      // Close the stream object 
      streamResponse.Close(); 

      // Release the HttpWebResponse 
      response.Close(); 

      Dispatcher.BeginInvoke(() => 
       { 
        //Update UI here 

       }); 
      } 
     } 
     catch (WebException e) 
     { 
      //Handle non success exception here     
     }     
}