【问题标题】:upload an image into mysql database using windows universal app使用 windows 通用应用程序将图像上传到 mysql 数据库
【发布时间】:2016-06-03 06:33:00
【问题描述】:

我一直在尝试使用 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 通用应用程序中不再可用 任何帮助将不胜感激

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 感谢您的回答,但我实际上迷路了,您能否给我提供更多详细信息。事实上,我想从我的应用程序中将新产品添加到 mydatabse 中,然后在我的 UWP 应用程序中显示这些产品。所以我需要选择一张图片(我认为使用文件选择器)并准备我的 php 文件以在 Mysql 中上传图片。
    • 是的,您可以使用filepicker从您的应用中选择一张图片,但选择的图片实际上是StorageFile,您可以将这个StorageFile转换为IRandomAccessStream imgstream = await file.OpenReadAsync();的流媒体,然后您可以上传此流。
    • 谢谢你的帮助......我试着按照你已经提到的那样做......但我被卡住了......你能给我完整的例子......
    猜你喜欢
    • 1970-01-01
    • 2019-05-31
    • 2016-05-26
    • 2011-07-18
    • 1970-01-01
    • 2015-04-17
    • 1970-01-01
    • 2018-08-10
    • 2015-01-01
    相关资源
    最近更新 更多