2013-07-04 182 views
1

我試圖上傳圖像到我的個人資料使用API​​,但我得到一個未知:NOT_FOUND 404錯誤。我使用的調用是/d2l/api/lp/1.0/profile/myProfile/image,我傳遞內容類型,長度和文件名(profileImage)。我將圖像作爲dataStream傳遞。我也縮小了圖像的大小。有任何想法嗎?valence desire 2學習配置文件圖像上傳

也在這裏是從入門例子

public void CallAction(ID2LUserContext userContext, int retryAttempts, string url, byte[] data, string method = "") 
    { 
     Uri uri = userContext.CreateAuthenticatedUri(url, method); 
     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     request.AllowAutoRedirect = false; 
     request.Method = method; 

     if (method.Equals("PUT") || method.Equals("POST")) 
     { 

      request.ContentType = "image/jpeg"; 
      //request.Headers.Add("Content-Disposition", "form-data; name=\"profileImage\"; filename=\"profileImage.jpg\""); 
      request.Headers.Add("Accept-Encoding", "gzip, deflate, compress"); 
      //request.Headers.Add("X-Upload-Content-Type", "image/jpg"); 
      //request.Headers.Add("X-Upload-Content-Length", data.Length.ToString()); 
      //request.Headers.Add("X-Upload-File-Name", "profileImage"); 
      request.ContentLength = data.Length; 

      Stream dataStream = request.GetRequestStream(); 
      dataStream.Write(data, 0, data.Length); 
      dataStream.Flush(); 
      dataStream.Close(); 
     } 

}

而且orginally得到當我運行一個GET取回我的照片也返回404錯誤我CallAction代碼的一部分。

+0

您的上傳[RFC1867](http://tools.ietf.org/html/rfc1867.html)是否符合? –

+0

您的代碼廢料使它看起來像您所提供的內容處置和「圖像/ JPEG」的Content-Type頭的HTTP POST頭,當那些應該被連接到*部分*頭。 該HTTP POST頭應包含「多部分形式/數據」的內容類型(與邊界),並且*部分*應包含文件的數據(與邊界),其具有的「Content-Disposition的」標頭( form-data)和'Content-Type'(圖像內容類型,在你的案例'image/jpeg'中)。 我不清楚你的代碼是這樣做的。 –

回答

0

我們在這裏測試了這個對我們的測試實例的調用,它確實有效。下面介紹一下HTTP數據包報頭看起來像一個有效的測試呼叫(從與蟒蛇requests模塊形成的調用):

{ 'User-Agent': 'python-requests/1.2.3 CPython/3.3.2 Darwin/12.4.0', 
    'Accept': '*/*', 
    'Content-Type': 'multipart/form-data; boundary=716acd781e224902854e6845bc62f653', 
    'Content-Length': '117886', 
    'Accept-Encoding': 'gzip, deflate, compress' } 

這個網址:

https://somelms.edu/d2l/api/lp/1.0/profile/myProfile/image? 
x_a={appId} 
&x_c=Lz3PDTaUgG46cMF3CajAsiiGzz0C6u5QTLieAmbONZ0 
&x_b={userId} 
&x_d=7sSqbce1_ictuNAs80n01h0jSI0YxxKbPM01W7f49a0 
&x_t={timestamp} 

與身體,看起來像這樣(注意在本體內的部分標頭,表徵單個部件中包含的圖像數據的體內的含量):

--716acd781e224902854e6845bc62f653 
Content-Disposition: form-data; name="profileImage"; filename="profileImage-student.png" 
Content-Type: image/png 

{image bytes here} 

--716acd781e224902854e6845bc62f653-- 
+0

謝謝,我添加了一些代碼和更多的信息到我原來的帖子,因爲我仍然有問題。 – user2521556

+0

查看我對你的問題的評論;我已經修改了這個答案,以便更清楚地知道哪些標題出現在哪裏。 –

0

此代碼應該解決您的問題:

public static void UploadFilesToRemoteUrl(byte[] profileImage, ID2LUserContext userContext, string accion) 
    { 
     //Reference: 
     //action = "/d2l/api/lp/1.3/profile/" + profileIdentifier + "/image"; 

     //profileImage = the profileImage of user read from disk: 
     /* 
     FileStream fileStream = new FileStream(pictureLocalPath, FileMode.Open, FileAccess.Read); 
     Byte[] img = new Byte[fileStream.Length]; 
     fileStream.Read(img, 0, Convert.ToInt32(img.Length)); 
     fileStream.Close(); 
     */ 

     var uri = userContext.CreateAuthenticatedUri(accion, "POST"); 
     string boundary = "bde472ff1f1a46539e54e655857c27c1"; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
     request.ContentType = "multipart/form-data; boundary=" + 
     boundary; 
     request.Headers.Add("Accept-Encoding", "gzip, deflate, compress"); 
     request.Method = "POST"; 
     request.KeepAlive = true; 

     request.Proxy.Credentials = new NetworkCredential(Constantes.UsuarioProxy, Constantes.PasswordProxy, Constantes.DominioProxy); 

     Stream memStream = new System.IO.MemoryStream(); 

     byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + 
     boundary + "\r\n"); 


     string formdataTemplate = "\r\n--" + boundary + 
     "\r\nContent-Disposition: form-data; name=\"profileImage\"; filename=\"profileImage.jpg\"\r\nContent-Type: image/jpeg;\r\n\r\n"; 

     byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formdataTemplate); 
     memStream.Write(formitembytes, 0, formitembytes.Length); 

     //escribo el array de byte de la imagen 
     memStream.Write(profileImage, 0, profileImage.Length); 

     byte[] boundaryClose = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--"); 
     memStream.Write(boundaryClose, 0, boundarybytes.Length); 

     StreamReader readerReq = new StreamReader(memStream); 
     string stringReq = readerReq.ReadToEnd(); 

     request.ContentLength = memStream.Length; 
     Stream requestStream = request.GetRequestStream(); 
     memStream.Position = 0; 
     byte[] tempBuffer = new byte[memStream.Length]; 
     memStream.Read(tempBuffer, 0, tempBuffer.Length); 
     memStream.Close(); 
     requestStream.Write(tempBuffer, 0, tempBuffer.Length); 
     requestStream.Close(); 

     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
     if (response.StatusCode == HttpStatusCode.OK) 
     { 
      StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8); 
      string responseValence = reader.ReadToEnd(); 
      response.Close(); 
     } 
    } 
+0

可不可以給你的代碼做一些什麼解釋?我們通常儘量避免讓帖子只包含代碼。你可以做到這一點無論在郵件的正文或整個代碼中的註釋:) – Dan