这是我第一次在 UWP 上开发,我无法使用 PHP 将图像添加到 Mysql 数据库中,我必须在数据库中添加它的路径。
我不能使用 sqlite 或任何其他数据库,因为我的同事已经开始使用 Mysql。 谁能帮我或举个例子 我真的很感谢你的帮助(我被困在这里)
所以,这是我用来从我的画廊上传所选图像的 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);
`
这是我正在使用的 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";
}
?>
我得到的只是我创建的文件夹中的空图像......显然我在从 UWP 上传图像后遇到了问题,但我不确定。
最佳答案
使用 PHP 读取文件并将其插入数据库。 请记住,图像越大,字符串越长。
$data = file_get_contents('/pics/item.png');
return $data;
这将返回数据(来自图片)
关于c# - 如何使用php将UWP中的图像保存到MYsql中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35950830/