valence desire 2学习个人资料图片上传错误404

时间:2014-01-30 20:34:44

标签: image upload profile desire2learn valence

我正在尝试使用api上传图像配置文件,但我收到一个未知:NOT_FOUND 404错误。我正在使用的电话是POST /d2l/api/lp/1.3/profile/(profileId)/image,我传递的是内容类型,长度和文件名(profileImage)。我将图像作为dataStream传递。我也缩小了图像的大小。有什么想法吗?

                public static void UploadFilesToRemoteUrl(string file, string logpath, NameValueCollection nvc, ID2LUserContext userContext, string accion)
    {
        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.png\" \r\nContent-Type: image/png\r\n";

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


        // Read image File *************************************************************
        FileStream fileStream = new FileStream(file, FileMode.Open,FileAccess.Read);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            memStream.Write(buffer, 0, bytesRead);
        }
        fileStream.Close();
        //*****************************************************************************
        //*********** End Read image file *********************************************

        memStream.Write(boundarybytes, 0, boundarybytes.Length);

        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();
        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
        string responseValence = reader.ReadToEnd();

    }

2 个答案:

答案 0 :(得分:0)

404最有可能来自

  • 您提供的API路由(并且后端服务无法将您的API路由与处理程序方法相匹配):这可能是因为profileId值不正确或错误输入路由,或路由中的API版本号不正确等等。

  • 由于某种原因,后端服务正在接受您的个人资料图片数据,但无法将其分配给用户的个人资料。

这是捕获上传的个人资料图像数据包的一些请求/响应详细信息。将配置文件图像上传到“我的配置文件”时,我使用从这些值构建的HTTP标头:

{'Content-Length': '75143', 
 'User-Agent': 'python-requests/2.2.1 CPython/3.3.3 Darwin/12.5.0', 
 'Content-Type': 'multipart/form-data; boundary=bde472ff1f1a46539e54e655857c27c1', 
 'Accept': '*/*', 
 'Accept-Encoding': 'gzip, deflate, compress'}

请注意,这是一个multipart / form-data内容主体,在单个身体部位周围有一个边界标记。请求的正文内容如下所示:

--bde472ff1f1a46539e54e655857c27c1
Content-Disposition: form-data; name="profileImage"; filename="profile_img-225x225.png"

[actual PNG bytes here]
--bde472ff1f1a46539e54e655857c27c1--

Content-Disposition标头中的name属性必须为profileImage,并且filename属性应以您用于提供内容的本地文件名命名(因此,就后端服务而言,它的价值并不是特别重要。)

最后,还有特定的角色权限,允许用户编辑某人其他人的个人资料图片,因此您应该确保API调用的调用用户上下文有权编辑其他人的图像

答案 1 :(得分: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();
        }
    }