简体   繁体   中英

how to save an image from UWP into MYsql using php

It's my first time developing on UWP and I can't add an image into Mysql database using PHP and I must add its' path in the database.

I can't use sqlite or any other database as my collegues started already using Mysql. Can anyone please help me or give an example I wouLd really appreciate your help (i'm stuck here )

So, here is my UWP code that I used to upload the chosen image from my gallery

private Stream stream = new MemoryStream();
        private CancellationTokenSource cts;

    public MainPage()
    {
        this.InitializeComponent();
    }

    private async void buttonUpload_Click(object sender, RoutedEventArgs e)
    {
        FileOpenPicker open = new FileOpenPicker();
        open.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        open.ViewMode = PickerViewMode.Thumbnail;

        // Filter to include a sample subset of file types
        open.FileTypeFilter.Clear();
        open.FileTypeFilter.Add(".bmp");
        open.FileTypeFilter.Add(".png");
        open.FileTypeFilter.Add(".jpeg");
        open.FileTypeFilter.Add(".jpg");

        // Open a stream for the selected file
        StorageFile file = await open.PickSingleFileAsync();

        // Ensure a file was selected
        if (file != null)
        {
            // Ensure the stream is disposed once the image is loaded
            using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read))
            {
                BitmapImage bitmapImage = new BitmapImage();
                await bitmapImage.SetSourceAsync(fileStream);
                fileStream.AsStream().CopyTo(stream);
                img.Source = bitmapImage;
            }
        }
    }

    private async void submit_Click(object sender, RoutedEventArgs e)
    {         
        Uri uri = new Uri("http://localhost/mydatabase/add.php");
        HttpClient client = new HttpClient();
        HttpStreamContent streamContent = new HttpStreamContent(stream.AsInputStream());
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
        request.Content = streamContent;
        HttpResponseMessage response = await client.PostAsync(uri, streamContent).AsTask(cts.Token);

        `

and here is the php i'm using

 <?php
 if($_SERVER['REQUEST_METHOD']=='POST'){
  $UserName = $_POST['UserName'];
 $UserImage = $_POST['UserImage'];
 require_once('conn.php');
 $sql ="SELECT  UserId FROM user";
 $res = mysqli_query($connect,$sql);
 $UserId =0 ;
 while($row = mysqli_fetch_array($res)){     
 $UserId = $row['UserId'];
 $UserId = $UserId+1;
 }
 $path = "UserImage/$UserId.png";
 $actualpath = "http://localhost/mydatabase/$path";
 $sql = "INSERT INTO user (UserId,UserName,UserImage) VALUES ('$UserId','$UserName','$actualpath')";
 if(mysqli_query($connect,$sql)){
 file_put_contents($path,base64_decode($UserImage));
 echo "Successfully Uploaded";
 } 
 mysqli_close($connect);
 }else{
 echo "Error";
 }
 ?>

And all what i get is empty images in the folder I created ... apparently I have a problem after uploading the image from the UWP, but I'm not sure about that.

Read the file using PHP and insert that into the database. Keep in mind, the bigger the image the longer the string.

$data = file_get_contents('/pics/item.png');

return $data;

This would return the data(from the picture)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM