Ejemplo n.º 1
0
        private async void UploadImage(StorageFile file)
        {
            try
            {
                pr_Upload.Visibility = Visibility.Visible;
                var url = "http://api.vc.bilibili.com/api/v1/image/upload?access_key={0}&appkey={1}&build=5250000&platform=android&src=bilih5&ts={2}";
                url  = string.Format(url, ApiHelper.access_key, ApiHelper.AndroidKey.Appkey, ApiHelper.GetTimeSpan_2);
                url += "&sign=" + ApiHelper.GetSign(url);
                IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);

                var bytes = new byte[fileStream.Size];
                await fileStream.ReadAsync(bytes.AsBuffer(), (uint)fileStream.Size, Windows.Storage.Streams.InputStreamOptions.None);

                var client  = new RestClient(url);
                var request = new RestRequest(Method.POST);
                request.AddFile("file_up", bytes, file.Name);
                IRestResponse response = await client.Execute(request);

                var content = response.Content;
                UploadImagesModel uploadImagesModel = JsonConvert.DeserializeObject <UploadImagesModel>(content);
                if (uploadImagesModel.code == 0)
                {
                    uploadImagesModel.data.image_size = (await file.GetBasicPropertiesAsync()).Size / 1024;
                    imgs.Add(uploadImagesModel.data);
                }
                else
                {
                    Utils.ShowMessageToast(uploadImagesModel.message);
                }
            }
            catch (Exception)
            {
                Utils.ShowMessageToast("图片上传失败");
            }
            finally
            {
                pr_Upload.Visibility = Visibility.Collapsed;
                if (gv_Pics.Items.Count != 0)
                {
                    pics.Visibility = Visibility.Visible;
                }
                else
                {
                    pics.Visibility = Visibility.Collapsed;
                }
                txt_PicCount.Text = gv_Pics.Items.Count.ToString();
            }
        }
Ejemplo n.º 2
0
        private async void UploadImage(StorageFile file)
        {
            try
            {
                pr_Upload.Visibility = Visibility.Visible;
                var url = "http://api.vc.bilibili.com/api/v1/image/upload?access_key={0}&appkey={1}&build=5250000&platform=android&src=bilih5&ts={2}";
                url  = string.Format(url, ApiHelper.AccessKey, ApiHelper.AndroidKey.Appkey, ApiHelper.TimeStamp2);
                url += "&sign=" + ApiHelper.GetSign(url);
                IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);

                var client        = new HttpClient();
                var streamContent = new StreamContent(fileStream.AsStream());
                streamContent.Headers.ContentDisposition.Name     = "file_up";
                streamContent.Headers.ContentDisposition.FileName = file.Name;
                var response = await client.PostAsync(url, streamContent);

                var content = await response.Content.ReadAsStringAsync();

                UploadImagesModel uploadImagesModel = JsonConvert.DeserializeObject <UploadImagesModel>(content);
                if (uploadImagesModel.code == 0)
                {
                    uploadImagesModel.data.image_size = (await file.GetBasicPropertiesAsync()).Size / 1024;
                    imgs.Add(uploadImagesModel.data);
                }
                else
                {
                    Utils.ShowMessageToast(uploadImagesModel.message);
                }
            }
            catch (Exception)
            {
                Utils.ShowMessageToast("图片上传失败");
            }
            finally
            {
                pr_Upload.Visibility = Visibility.Collapsed;
                if (gv_Pics.Items.Count != 0)
                {
                    pics.Visibility = Visibility.Visible;
                }
                else
                {
                    pics.Visibility = Visibility.Collapsed;
                }
                txt_PicCount.Text = gv_Pics.Items.Count.ToString();
            }
        }