2011-03-15 153 views
1

我的代碼想要將圖片上傳到服務器,如下所示,但它總是失敗。你知道爲什麼嗎?Windows Phone 7如何上傳圖片到服務器

public static void SendRequest(System.Text.StringBuilder sReq, byte[] sbyteData, Action<UpLoadPicData, int> onEventResponse = null, Action onFinally = null) 
    { 
     WebClient wc = new WebClient(); 

     wc.OpenWriteCompleted += new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted); 
     Uri u = new Uri(sReq.ToString()); 
     wc.Headers[HttpRequestHeader.ContentLength] = sReq.Length.ToString(); 
     wc.Headers[HttpRequestHeader.Accept] = "*/*"; 
     wc.Headers[HttpRequestHeader.ContentType] = "application/octet-stream"; 

     wc.OpenWriteAsync(u, "POST", sbyteData); 
    } 

    public static void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      object[] objArr = e.UserState as object[]; 
      byte[] fileContent = e.UserState as byte[]; 

      Stream outputStream = e.Result; 
      outputStream.Write(fileContent, 0, fileContent.Length); 
      outputStream.Flush(); 
      outputStream.Close(); 

     } 
    } 
+0

它是如何失敗?是否有例外? – 2011-03-15 12:09:19

回答

1

這樣做與WebClient這將是非常棘手(我不知道是否有可能)在手機上。改爲使用HttpWebRequest

看一看關於同一主題的這些其他問題:

Uploading an image using C# and WebRequest?

Upload files with HTTPWebrequest (multipart/form-data)

+0

我試過HttWebRequest,alwasy失敗,錯誤= {「遠程服務器返回錯誤:NotFound。」} – Eric 2011-03-16 09:01:57

+0

我注意到http頭中有「Referer」項,我不明白爲什麼。 – Eric 2011-03-16 09:02:33

3
void photoChooserTask_Completed(object sender, PhotoResult e) 
    { 
     if (e.TaskResult == TaskResult.OK) 
     { 
      System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage(); 
      bmp.SetSource(e.ChosenPhoto); 
      image1.Source = bmp; 
      byte[] sbytedata = ReadToEnd(e.ChosenPhoto); 
      string s = sbytedata.ToString(); 
      WebClient wc = new WebClient(); 
      Uri u = new Uri("url here"); 
      wc.OpenWriteCompleted+=new OpenWriteCompletedEventHandler(wc_OpenWriteCompleted); 
      wc.OpenWriteAsync(u, "POST", sbytedata); 

     } 
    } 
    public static void wc_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e) 
    { 
     if (e.Error == null) 
     { 
      object[] objArr = e.UserState as object[]; 
      byte[] fileContent = e.UserState as byte[]; 

      Stream outputStream = e.Result; 
      outputStream.Write(fileContent, 0, fileContent.Length); 
      outputStream.Flush(); 
      outputStream.Close(); 
      string s = e.Result.ToString(); ; 

     } 
    } 
    public static byte[] ReadToEnd(System.IO.Stream stream) 
    { 
     long originalPosition = stream.Position; 
     stream.Position = 0; 

     try 
     { 
      byte[] readBuffer = new byte[4096]; 

      int totalBytesRead = 0; 
      int bytesRead; 

      while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) 
      { 
       totalBytesRead += bytesRead; 

       if (totalBytesRead == readBuffer.Length) 
       { 
        int nextByte = stream.ReadByte(); 
        if (nextByte != -1) 
        { 
         byte[] temp = new byte[readBuffer.Length * 2]; 
         Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length); 
         Buffer.SetByte(temp, totalBytesRead, (byte)nextByte); 
         readBuffer = temp; 
         totalBytesRead++; 
        } 
       } 
      } 

      byte[] buffer = readBuffer; 
      if (readBuffer.Length != totalBytesRead) 
      { 
       buffer = new byte[totalBytesRead]; 
       Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); 
      } 
      return buffer; 
     } 
     finally 
     { 
      stream.Position = originalPosition; 
     } 
    } 

我用你的代碼...和它的工作..我還附上轉換爲字節[]從圖像源到此..圖像取自圖庫

+0

雖然請注意,如果圖片上傳時沒有迴應,但它實際上傳到服務器的背景中。 – 2011-11-09 09:52:51