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.
这是我第一次在UWP上开发,我无法使用PHP将图像添加到Mysql数据库中,我必须在数据库中添加其路径。
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 )
我不能使用sqlite或任何其他数据库,因为我的同事已经开始使用Mysql。任何人都可以帮助我或举个例子我非常感谢你的帮助(我被困在这里)
So, here is my UWP code that I used to upload the chosen image from my gallery
所以,这是我用来从我的画廊上传所选图像的UWP代码
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
<?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.
所有我得到的是我创建的文件夹中的空图像...显然我从UWP上传图像后出现问题,但我不确定。
1 个解决方案
#1
0
Read the file using PHP and insert that into the database. Keep in mind, the bigger the image the longer the string.
使用PHP读取文件并将其插入数据库。请记住,图像越大,字符串越长。
$data = file_get_contents('/pics/item.png');
return $data;
This would return the data(from the picture)
这将返回数据(来自图片)
#1
0
Read the file using PHP and insert that into the database. Keep in mind, the bigger the image the longer the string.
使用PHP读取文件并将其插入数据库。请记住,图像越大,字符串越长。
$data = file_get_contents('/pics/item.png');
return $data;
This would return the data(from the picture)
这将返回数据(来自图片)