使用Windows通用应用程序将图像上传到mysql数据库

时间:2016-02-21 23:14:10

标签: c# php mysql xaml win-universal-app

我一直试图使用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>`

这是我的mainpage.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手机密码而陷入困境,但我无法找到

的替代品

byte[] insertuser= client.uploadValues("",values); client.Headers.Add("content_type","binary/octet_stream"); byte[] insertUserImage=client.uploadFile("",FileChooser.FileName) ;

似乎这些方法在Windows通用应用程序中不再可用 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

对于UWP应用,如果您要上传图片,则需要将此图片转换为流,并将此流用作HttpRequestMessage的内容。

有一个官方HttpClient sample,此示例中的方案5是关于使用HTTP POST命令将流上传到服务器。

此示例中还包含服务器代码,您可以看到如何解析服务器的上传流。