2

WebAPI를 사용하여 Windows Phone 8 앱에서 SQL 서버 데이터베이스로 이미지를 업로드하려고합니다. 이미지 용 모델 클래스를 사용하고 있습니다.이 모델은 이미지가있는 항목에 속한 ID, 이미지의 이름 및 이미지 자체를 보유하는 바이트 배열로 구성됩니다. WebClient 개체를 사용하여 WebAPI 메서드에 액세스합니다. 이미지를 업로드하려고하면 아래와 같이 예외가 발생합니다. 누구나 왜 오류가 있는지 생각할 수 있습니까? 또한 SQL 데이터베이스에 이미지를 저장하는 다른 방법에 대해서도 열려 있습니다. 모양을 가져 주셔서 감사합니다! 코드모델을 사용하여 ASP.NET WebAPI를 사용하여 이미지 업로드

private MemoryStream photoStream; 

...

private void upload() 
     { 
      try 
      { 
       Images image = new Images(); 
       image.ImagesBytes = photoStream.ToArray(); 
       image.ImagesID = 3; 
       image.ImagesCaption = "this is a test"; 

       string jsonData = JsonConvert.SerializeObject(image); 

       WebClient webClient = new WebClient(); 
       webClient.Headers["Content-type"] = "application/json"; 
       webClient.Encoding = Encoding.UTF8; 

       Uri uri = new Uri("http://myIP/api/Images/", UriKind.Absolute); 
       webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted); 
       webClient.UploadStringAsync(uri, "GET", jsonData); 
      } 
      catch 
      { 
       // Display the Uploaded message 
       tbError.Visibility = Visibility.Visible; 
      } 
     } 

....

 void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) 
     { 
      try 
      { 
       Images image = JsonConvert.DeserializeObject<Images>(e.Result); 
      } 
      catch (Exception ex) 
      { 
       // Display the Uploaded message 
       tbError.Visibility = Visibility.Visible; 
      } 
     } 

예외

,
System.Net.ProtocolViolationException: Operation is not valid due to the current state of the object. 
    at System.Net.Browser.ClientHttpWebRequest.InternalBeginGetRequestStream(AsyncCallback callback, Object state) 
    at System.Net.Browser.ClientHttpWebRequest.BeginGetRequestStream(AsyncCallback callback, Object state) 
    at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Byte[] header, Byte[] footer, CompletionDelegate completionDelegate, AsyncOperation asyncOp) 
    at System.Net.WebClient.UploadDownloadBits(WebRequest request, Stream readStream, Stream writeStream, Byte[] buffer, Byte[] header, Byte[] footer, CompletionDelegate upCompletionDelegate, CompletionDelegate downCompletionDelegate, AsyncOperation asyncOp) 
    at System.Net.WebClient.UploadStringAsync(Uri address, String method, String data, Object userToken) 

답변

1

귀하의 HTTP 방법이 잘못되었다고 생각합니다. 대신

...

webClient.UploadStringAsync(uri, "GET", jsonData); 

시도 ...

webClient.UploadStringAsync(uri, "POST", jsonData); 
+0

와우! 감사합니다 @ Rick Rainey, 중대한 반점! 이번엔 그걸 알아 채지 못했다 니 믿을 수가 없어. –

관련 문제