How to upload some files of any type to online server by sending data to PHP API using HttpClient in C# (Windows Form Application)? [duplicate]

落花浮王杯 提交于 2020-01-05 06:09:47

问题


My question resembles a lot of question but I have seen that my question is different. I wanted to apply CRUD operation on file uploading using C#(Windows Form Application). Here is the code I wanted to use...

Code:

async void uploadImage(string url, string api_key, string device_token, string device_type, string email,string file_name)
    {
        IEnumerable<KeyValuePair<string, string>> queries = new List<KeyValuePair<string, string>>()
        {
            new KeyValuePair<string, string>("email",email),
           // new KeyValuePair<string, string>("files",file_name),
            new KeyValuePair<string, string>("api_key",api_key),
            new KeyValuePair<string, string>("device_token",device_token),
            new KeyValuePair<string, string>("device_type",device_type)
        };
        HttpContent q = new FormUrlEncodedContent(queries);
        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage response = await client.PostAsync(url, q))
            {
                using (HttpContent content = response.Content)
                {
                    try
                    {
                        var mycontent = await content.ReadAsStringAsync();
                        var jo = JObject.Parse(mycontent);
                        string errorStatus = jo["Result"]["error"].ToString();
                        string message = jo["Result"]["message"].ToString();
                        if (errorStatus == "False")
                        {
                            MessageBox.Show(message);
                        }
                        else
                        {
                            MessageBox.Show(message);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }
    }

Please help me, how can I do that? Your cooperation will be highly Appreciated

来源:https://stackoverflow.com/questions/52539590/how-to-upload-some-files-of-any-type-to-online-server-by-sending-data-to-php-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!