1

我正在尝试使用 api 将图像上传到我的个人资料,但出现 Unknown: NOT_FOUND 404 错误。我正在使用的调用是/d2l/api/lp/1.0/profile/myProfile/image,我正在传递内容类型、长度和文件名(profileImage)。我将图像作为数据流传递。我也缩小了图像的大小。有任何想法吗?

这里也是我的 CallAction 代码的一部分,最初是从 Getting Started 示例中获得的

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();
        }

}

此外,当我运行 get 来检索我的照片时,它也会返回 404 错误。

4

2 回答 2

0

我们在这里针对我们的测试实例测试了这个调用,它确实有效。requests以下是有效测试调用的 HTTP 数据包标头(来自使用 python模块形成的调用)的样子:

{ '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--
于 2013-07-05T18:33:51.160 回答
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();
        }
    }
于 2014-02-03T18:15:56.850 回答