我正在使用Xamarin.Forms PCL应用程序,用户可以在其中选择照片并上传。我想将所选照片发送到网络服务,以便可以检查其正确格式和文件大小,然后将其上传到Google照片。
要将其传输到我的Web服务,它需要是一个字符串。我尝试使用
MediaFile file;
var stream = file.GetStream();
var bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
string content = System.Convert.ToBase64String(bytes);
我的第一个问题是我不知道将文件初始化为什么,以使其正确转换。一旦用户选择了一个图像,它将被存储在ram中。ImageSource image_source;
一旦上传,它就可以到达我的PHP网站,并且我得到了 $image = $_POST['image_string'];
我的第二个问题是如何将其转换回图像以检查文件类型和图像大小?
它通过以下方式发送到网站
var values = new Dictionary<string, string>
{
{"session", UserData.session },
{"image", ImageAsBase64().Result.Length.ToString() }
};
var content = new FormUrlEncodedContent(values);
var response = await App.client.PostAsync(WebUtils.URL, content);
var responseString = await response.Content.ReadAsStringAsync();
string page_result = responseString;
相关分类