コード例 #1
0
        private void UploadPhoto(Stream photoStream,
                                 string getServerMethodName,
                                 Dictionary <string, string> parameters,
                                 string saveMethodName,
                                 bool saveReturnsList,
                                 Action <VKBackendResult <VKPhoto> > callback,
                                 Action <double> progressCallback)
        {
            var vkParams = new VKRequestParameters(getServerMethodName,
                                                   parameters);

            var getServerRequest = new VKRequest(vkParams);

            getServerRequest.Dispatch <VKUploadServerAddress>(
                (res) =>
            {
                if (res.ResultCode == VKResultCode.Succeeded)
                {
                    var uploadUrl = res.Data.upload_url;

                    VKHttpRequestHelper.Upload(
                        uploadUrl,
                        photoStream,
                        "file1",
                        "image",
                        (uploadRes) =>
                    {
                        if (uploadRes.IsSucceeded)
                        {
                            var serverPhotoHashJson = uploadRes.Data;

                            var uploadData = JsonConvert.DeserializeObject <VKUploadResponseData>(serverPhotoHashJson);

                            if (!string.IsNullOrWhiteSpace(uploadData.server))
                            {
                                parameters["server"] = uploadData.server;
                            }
                            if (!string.IsNullOrWhiteSpace(uploadData.photos_list))
                            {
                                parameters["photos_list"] = uploadData.photos_list;
                            }
                            if (!string.IsNullOrWhiteSpace(uploadData.hash))
                            {
                                parameters["hash"] = uploadData.hash;
                            }
                            if (!string.IsNullOrWhiteSpace(uploadData.photo))
                            {
                                parameters["photo"] = uploadData.photo;
                            }

                            var saveWallPhotoVKParams = new VKRequestParameters(saveMethodName,
                                                                                parameters);

                            var saveWallPhotoRequest = new VKRequest(saveWallPhotoVKParams);

                            saveWallPhotoRequest.Dispatch(
                                callback,
                                (jsonStr) =>
                            {
                                if (saveReturnsList)
                                {
                                    var resp = JsonConvert.DeserializeObject <GenericRoot <List <VKPhoto> > >(jsonStr);

                                    return(resp.response.First());
                                }
                                else
                                {
                                    var resp = JsonConvert.DeserializeObject <GenericRoot <VKPhoto> >(jsonStr);

                                    return(resp.response);
                                }
                            });
                        }
                        else
                        {
                            callback(new VKBackendResult <VKPhoto> {
                                ResultCode = VKResultCode.UnknownError
                            });
                        }
                    },
                        progressCallback,
                        "Image.jpg");
                }
                else
                {
                    callback(new VKBackendResult <VKPhoto> {
                        ResultCode = res.ResultCode, Error = res.Error
                    });
                }
            });
        }