使用Windows Universal App将图像上传到MySQL数据库
本文关键字:MySQL 数据库 图像 Windows Universal App 使用 | 更新日期: 2023-09-27 18:35:33
我一直在尝试使用Windows通用应用程序和2个PHP文件将手机中的图像插入MySQL数据库。这是我的 XAML 代码'
<TextBox x:Name="UserName" HorizontalAlignment="Left" Margin="143,23,0,0" TextWrapping="Wrap" Text="nameB" VerticalAlignment="Top" Height="2" Width="174"/>
<TextBox x:Name="UserMail" HorizontalAlignment="Left" Margin="151,85,0,0" TextWrapping="Wrap" Text="emailB" VerticalAlignment="Top" Height="2" Width="174"/>
<TextBox x:Name="UserImage" HorizontalAlignment="Left" Margin="153,218,0,0" TextWrapping="Wrap" Text="imageB" VerticalAlignment="Top" Height="2" Width="119"/>
<PasswordBox x:Name="UserPassword" HorizontalAlignment="Left" Margin="185,145,0,0" VerticalAlignment="Top" Height="2" Width="141"/>
<Button x:Name="UploadImage" Content="upload" HorizontalAlignment="Left" Margin="284,218,0,0" VerticalAlignment="Top" Click="upload_Click"/>
<Button x:Name="SubmitUser" Content="submit" HorizontalAlignment="Left" Margin="242,297,0,0" VerticalAlignment="Top" Click="submit_Click"/>
</Grid>`
这是我的主页.xaml.cs
public sealed partial class MainPage : Page
{
HttpClient client = new HttpClient();
public MainPage()
{
this.InitializeComponent();
}
private async void upload_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
UserImage.Text = file.Name;
}
}
private async void submit_Click(object sender, RoutedEventArgs e)
{
/* NameValueCollection UserInfo = new NameValueCollection();
UserInfo.Add("UserName", UserName.Text);
UserInfo.Add("UserMail", UserMail.Text);
UserInfo.Add("UserPassword", UserPassword.Password);
UserInfo.Add("UserImage", UserImage.Text);
*/
///*********/////
String url = "http://172.19.241.135/tutorial/insertStudent.php";
var values = new List<KeyValuePair<String, String>>
{
new KeyValuePair<string, string>("UserName",UserName.Text),
new KeyValuePair<string, string>("UserMail",UserMail.Text),
new KeyValuePair<string, string>("UserPassword",UserPassword.Password),
new KeyValuePair<string, string>("UserImage",UserImage.Text)
};
HttpResponseMessage response = new HttpResponseMessage();
try
{
response = await client.PostAsync(url, new FormUrlEncodedContent(values));
if (response.IsSuccessStatusCode)
{
Debug.WriteLine(response.StatusCode.ToString());
var dialog = new MessageDialog("added succesfully ");
await dialog.ShowAsync();
}
else
{
// problems handling here
string msg = response.IsSuccessStatusCode.ToString();
throw new Exception(msg);
}
}
catch (Exception exc)
{
// .. and understanding the error here
Debug.WriteLine(exc.ToString());
}
}
}
我现在被困住了,因为我试图使用 Windows Phone 代码,但我找不到替代品
byte[] insertuser= client.uploadValues("",values);
client.Headers.Add("content_type","binary/octet_stream");
byte[] insertUserImage=client.uploadFile("",FileChooser.FileName) ;
似乎这些方法在Windows通用应用程序中不再可用任何帮助将不胜感激
对于 UWP 应用,如果要上传图像,则需要将此图像转换为流,并将此流用作 HttpRequestMessage 的内容。
有一个官方的 HttpClient 示例,此示例中的方案 5 是关于使用 HTTP POST 命令将流上传到服务器。
此示例中还包括服务器的代码,您可以了解如何解析服务器的上传流。