Xamarin Forms 将图像上传到网页 POST
Xamarin Forms Upload image to webpage as POST
我有问题。我想将图像上传到网页,以便将其存储在我的服务器上。现在我可以找到很多上传文件到服务器的例子,但是图片呢?
我已经尝试创建图像的字节数组并将其作为 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);
还有其他方法吗?
您可以尝试以下方法:
//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);
}
我有问题。我想将图像上传到网页,以便将其存储在我的服务器上。现在我可以找到很多上传文件到服务器的例子,但是图片呢?
我已经尝试创建图像的字节数组并将其作为 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);
还有其他方法吗?
您可以尝试以下方法:
//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);
}