首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >valence desire 2学习资料图片上传

valence desire 2学习资料图片上传
EN

Stack Overflow用户
提问于 2013-07-04 22:54:06
回答 2查看 297关注 0票数 1

我正在尝试使用应用程序接口将图片上传到我的个人资料中,但是我得到了一个未知的: NOT_FOUND 404错误。我使用的调用是/d2l/api/lp/1.0/profile/myProfile/image,我传递的是内容类型、长度和文件名(profileImage)。我将图像作为dataStream传递。我也缩小了图像的大小。有什么想法吗?

下面是我从入门示例中获得的CallAction代码的一部分

代码语言:javascript
复制
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错误。

EN

回答 2

Stack Overflow用户

发布于 2013-07-06 02:33:51

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

代码语言:javascript
复制
{ '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' }

到此URL:

代码语言:javascript
复制
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}

正文如下所示(请注意正文中的部分标头,它描述了包含图像数据的正文中单个部分的内容):

代码语言:javascript
复制
--716acd781e224902854e6845bc62f653
Content-Disposition: form-data; name="profileImage"; filename="profileImage-student.png"
Content-Type: image/png

{image bytes here}

--716acd781e224902854e6845bc62f653--
票数 0
EN

Stack Overflow用户

发布于 2014-02-04 02:15:57

这段代码应该可以解决你的问题:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17473294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档