Esempio n. 1
0
        public static async Task <string> headUpload(string stunum, string fileUri, string uri = "http://hongyan.cqupt.edu.cn/cyxbsMobile/index.php/home/Photo/upload", bool isPath = false)
        {
            Windows.Web.Http.HttpClient _httpClient = new Windows.Web.Http.HttpClient();
            CancellationTokenSource     _cts        = new CancellationTokenSource();

            Windows.Web.Http.HttpStringContent stunumStringContent = new Windows.Web.Http.HttpStringContent(stunum);
            string head = "";
            //IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            IStorageFile saveFile;

            if (isPath)
            {
                saveFile = await StorageFile.GetFileFromPathAsync(fileUri);
            }
            else
            {
                saveFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(fileUri));
            }

            try
            {
                // 构造需要上传的文件数据
                IRandomAccessStreamWithContentType stream1 = await saveFile.OpenReadAsync();

                Windows.Web.Http.HttpStreamContent            streamContent = new Windows.Web.Http.HttpStreamContent(stream1);
                Windows.Web.Http.HttpMultipartFormDataContent fileContent   = new Windows.Web.Http.HttpMultipartFormDataContent();

                fileContent.Add(streamContent, "fold", "head.png");
                fileContent.Add(stunumStringContent, "stunum");

                Windows.Web.Http.HttpResponseMessage response =
                    await
                    _httpClient.PostAsync(new Uri(uri), fileContent)
                    .AsTask(_cts.Token);

                head = Utils.ConvertUnicodeStringToChinese(await response.Content.ReadAsStringAsync().AsTask(_cts.Token));
                Debug.WriteLine(head);
                return(head);
            }
            catch (Exception)
            {
                Debug.WriteLine("上传头像失败,编辑页面");
                return("");
            }
        }
Esempio n. 2
0
        public static async Task<string> headUpload(string stunum, string fileUri, string uri = "http://hongyan.cqupt.edu.cn/cyxbsMobile/index.php/home/Photo/upload", bool isPath = false)
        {
            Windows.Web.Http.HttpClient _httpClient = new Windows.Web.Http.HttpClient();
            CancellationTokenSource _cts = new CancellationTokenSource();
            Windows.Web.Http.HttpStringContent stunumStringContent = new Windows.Web.Http.HttpStringContent(stunum);
            string head = "";
            //IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
            IStorageFile saveFile;
            if (isPath)
                saveFile = await StorageFile.GetFileFromPathAsync(fileUri);
            else
                saveFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(fileUri));

            try
            {
                // 构造需要上传的文件数据
                IRandomAccessStreamWithContentType stream1 = await saveFile.OpenReadAsync();
                Windows.Web.Http.HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(stream1);
                Windows.Web.Http.HttpMultipartFormDataContent fileContent = new Windows.Web.Http.HttpMultipartFormDataContent();

                fileContent.Add(streamContent, "fold", "head.png");
                fileContent.Add(stunumStringContent, "stunum");

                Windows.Web.Http.HttpResponseMessage response =
                    await
                        _httpClient.PostAsync(new Uri(uri), fileContent)
                            .AsTask(_cts.Token);
                head = Utils.ConvertUnicodeStringToChinese(await response.Content.ReadAsStringAsync().AsTask(_cts.Token));
                Debug.WriteLine(head);
                return head;
            }
            catch (Exception)
            {
                Debug.WriteLine("上传头像失败,编辑页面");
                return "";
            }
        }