Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Xamarin表单将图像作为帖子上传到网页_C#_Xamarin_Xamarin.forms_Httpwebrequest_Httpclient - Fatal编程技术网

C# Xamarin表单将图像作为帖子上传到网页

C# Xamarin表单将图像作为帖子上传到网页,c#,xamarin,xamarin.forms,httpwebrequest,httpclient,C#,Xamarin,Xamarin.forms,Httpwebrequest,Httpclient,我有个问题。我想上传一张图片到一个网页,这样我就可以把它存储在我的服务器上。现在我可以找到很多将文件上传到服务器的例子,但是图像呢 我已经尝试创建图像的字节数组并将其作为POST发送,但在下面的一行中崩溃,表示URI太长: var postData = new List<KeyValuePair<string, string>>(); postData.Add(new KeyValuePair<string, string>("id", App.User.Id

我有个问题。我想上传一张图片到一个网页,这样我就可以把它存储在我的服务器上。现在我可以找到很多将文件上传到服务器的例子,但是图像呢

我已经尝试创建图像的字节数组并将其作为POST发送,但在下面的一行中崩溃,表示URI太长:

var postData = new List<KeyValuePair<string, string>>();
postData.Add(new KeyValuePair<string, string>("id", App.User.Id.ToString()));
postData.Add(new KeyValuePair<string, string>("image", ByteArray));
var content = new FormUrlEncodedContent(postData);
var postData=newlist();
添加(新的KeyValuePair(“id”,App.User.id.ToString());
添加(新的KeyValuePair(“image”,ByteArray));
var content=newformurlencodedcontent(postData);

还有其他方法吗?

您可以尝试以下方法:

   //In Xamarin
        public void UploadImage()
        {
            //create an instance of webclient
            System.Net.WebClient Client = new System.Net.WebClient();

            //add the header
            Client.Headers.Add("Content-Type", "binary/octet-streOpenWriteam");

            //uploads the file and gets the result in a byte[]
            // change `weburl/controller/actionmethod` to our weburl 
            byte[] result = Client.UploadFile("weburl/controller/actionmethod", "POST", "pathToFile");

            //converts the result[] to a string
            string resultString = System.Text.Encoding.UTF8.GetString(result, 0, result.Length);
        }
有关更多详细信息,您可以查看: