Пример #1
0
        public async Task <VkUploadDocumentResponse> Upload(string url, string fileName, Stream docStream)
        {
            var client = new HttpClient();

            string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            var    content  = new MultipartFormDataContent(boundary);

            var fileContent = new StreamContent(docStream);

            fileContent.Headers.ContentType        = new MediaTypeHeaderValue("application/octet-stream");
            fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = "\"" + fileName + "\"",
                Name     = "\"file\""
            };

            content.Add(fileContent);

            var responseMessage = await client.PostAsync(new Uri(url), content);

            byte[] bytes = await responseMessage.Content.ReadAsByteArrayAsync();

            Encoding encoding = Encoding.UTF8; //VK return windows-1251 which causes exception on Win10
            string   response = encoding.GetString(bytes, 0, bytes.Length);

            Debug.WriteLine("VkLib upload response: " + response);

            var json = JObject.Parse(response);

            return(VkUploadDocumentResponse.FromJson(json));
        }
        public static VkUploadDocumentResponse FromJson(JObject json)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            var result = new VkUploadDocumentResponse();

            result.File = (string)json["file"];

            return(result);
        }