使用 Windows 通用应用程序将图像上传到 mysql 数据库 [英] upload an image into mysql database using windows universal app

查看:52
本文介绍了使用 Windows 通用应用程序将图像上传到 mysql 数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用 Windows 通用应用程序和 2 个 php 文件将我手机中的图像插入到 mysql 数据库中.这是我的 xaml 代码`

i've been trying to insert an image from my phone into mysql database using a windows universal app and 2 php files . here is my xaml code `

    <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

and here is my 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 手机代码,但我找不到

and i'm stuck now as i was trying to use a windows phone code , but i can't find a replacement for

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

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

it seems that these methods are no more available in windows universal apps any help would be appreciated

推荐答案

对于 UWP app,如果你想上传一张图片,你需要把这张图片转成流,并使用这个流作为 HttpRequestMessage.

For UWP app, if you want to upload an image, you will need to convert this image to stream, and use this stream as the content of HttpRequestMessage.

有一个官方的 HttpClient 示例,本示例中的场景 5 是关于使用 HTTP POST 命令将流上传到服务器.

There is an official HttpClient sample, scenario 5 in this sample is about using HTTP POST command to upload a stream to a server.

服务器的代码也包含在这个示例中,您可以看到如何为服务器解析上传的流.

The code for server is also included in this sample, you can see how to resolve the uploaded stream for the server.

这篇关于使用 Windows 通用应用程序将图像上传到 mysql 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆