upload an image into mysql database using windows

2019-08-15 02:01发布

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>`

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());
        }



    }
}

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) ;

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

1条回答
Melony?
2楼-- · 2019-08-15 02:20

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.

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.

查看更多
登录 后发表回答