C# Xamarin表单将图像作为帖子上传到网页
我有个问题。我想上传一张图片到一个网页,这样我就可以把它存储在我的服务器上。现在我可以找到很多将文件上传到服务器的例子,但是图像呢 我已经尝试创建图像的字节数组并将其作为POST发送,但在下面的一行中崩溃,表示URI太长: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
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);
}
有关更多详细信息,您可以查看: