php - 如何将flutter上的图像上传到MySQL
问题描述
我是一个颤抖的新手。有人可以告诉我如何将图像上传到我的 MySQL 吗?在过去的几周里,我找不到任何解决方案。
我的代码:
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as HTTP;
import 'package:image_picker/image_picker.dart';
import 'package:phpfood/OwnerPage.dart';
import 'globals.dart' as globals;
import 'dart:async';
class AddProduct extends StatefulWidget {
@override
_AddProductState createState() => _AddProductState();
}
class _AddProductState extends State<AddProduct> {
TextEditingController controllerName = new TextEditingController();
TextEditingController controllerPrice = new TextEditingController();
TextEditingController controllerType = new TextEditingController();
File _image;
Future getImageGallery() async{
// ignore: deprecated_member_use
var imageFile = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = imageFile;
});
}
Future getImageCamera() async{
// ignore: deprecated_member_use
var imageFile = await ImagePicker.pickImage(source: ImageSource.camera);
setState(() {
_image = imageFile;
});
}
Future addProduct() async{
var url = 'http://10.0.2.2/foodsystem/addproduct.php';
//String base64Image = base64Encode(_image.readAsBytesSync());
//String fileName = _image.path.split("/").last;
http.post(url, body: {
"productname": controllerName.text,
"productprice": controllerPrice.text,
"producttype": controllerType.text,
"product_owner": globals.restaurantId,
//"image": base64Image,
//"imageName": fileName,
"image": _image.path,
//"image": _image.path.split('/').last,
});
}
/* Future addProduct(File imageFile) async{
// ignore: deprecated_member_use
var stream= new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var length= await imageFile.length();
var uri = Uri.parse("http://10.0.2.2/foodsystem/uploadg.php");
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile("image", stream, length, filename: basename(imageFile.path));
request.files.add(multipartFile);
request.fields['productname'] = controllerName.text;
request.fields['productprice'] = controllerPrice.text;
request.fields['producttype'] = controllerType.text;
request.fields['product_owner'] = globals.userId;
var respond = await request.send();
if(respond.statusCode==200){
print("Image Uploaded");
}else{
print("Upload Failed");
}
}*/
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("ADD PRODUCT"),),
body: Padding(
padding: const EdgeInsets.all(10.0),
child: ListView(
children: [
new Column(
children: [
SizedBox(
height: 25,
),
Container(
height: 230,
width: 250,
child: Center(
child: _image == null
? new Text("No Image Selected!") : new Image.file(_image),
),
),
Padding(
padding: const EdgeInsets.only(left: 110),
child: Row(
children: [
RaisedButton(
child: Icon(Icons.image),
color: Colors.red.shade200,
onPressed: getImageGallery,
),
RaisedButton(
child: Icon(Icons.camera_alt),
color: Colors.red.shade200,
onPressed: getImageCamera,
),
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Product Name", style: TextStyle(fontSize: 18),),
),
Padding(
padding: const EdgeInsets.only(left:10,right:10,top:0,bottom:0 ),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(15)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: controllerName,
textAlign: TextAlign.center,
decoration: InputDecoration(
hintText: 'Nasi Lemak Kerang',
border: InputBorder.none,
),
),
),
),
),
/*new TextField(
controller: controllerName,
decoration: new InputDecoration(
hintText: "Nasi Lemak Kerang",
),
),*/
SizedBox(
height: 10,
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Product Price (RM)", style: TextStyle(fontSize: 18),),
),
Padding(
padding: const EdgeInsets.only(left:10,right:10,top:0,bottom:0 ),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(15)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: controllerPrice,
textAlign: TextAlign.center,
autocorrect: true,
decoration: InputDecoration(
border: InputBorder.none,
counterText: '',
hintText: '12'),
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
//LengthLimitingTextInputFormatter(11)
],
maxLength: 11,
),
),
),
),
/*new TextField(
controller: controllerPrice,
decoration: new InputDecoration(
hintText: "12",
),
),*/
Padding(
padding: const EdgeInsets.all(8.0),
child: Text("Product Type", style: TextStyle(fontSize: 18),),
),
Padding(
padding: const EdgeInsets.only(left:10,right:10,top:0,bottom:0 ),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(15)),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
controller: controllerType,
textAlign: TextAlign.center,
decoration: InputDecoration(
hintText: 'breakfast, lunch, dinner',
border: InputBorder.none,
),
),
),
),
),
/*new TextField(
controller: controllerType,
decoration: new InputDecoration(
hintText: "breakfast, lunch, dinner",
),
),*/
new Padding(padding: const EdgeInsets.all(10.0),),
/*new RaisedButton(
child: new Text("ADD PRODUCT"),
color: Colors.red,
onPressed: (){
addProduct(_image);
}
),*/
new RaisedButton(
child: new Text("ADD PRODUCT"),
color: Colors.red,
onPressed: (){
addProduct();
Navigator.of(context).push(
MaterialPageRoute(builder: (BuildContext context)=> OwnerPage(username: globals.userName, user_id: globals.userId)));
},
),
/*FloatingActionButton(
onPressed: (){
return showDialog(
context: context,
builder: (context){
return AlertDialog(
content: Text(controllerPrice.text),
);
},
);
},
)*/
],
),
],
),
),
);
}
}
我的 PHP 代码:
<?php
include 'conn.php';
$product_owner = $_POST['product_owner'];
$productname = $_POST['productname'];
$productprice = $_POST['productprice'];
$producttype = $_POST['producttype'];
$image = $_POST['image'];
//$realImage = base64_decode($image);
$connect->query("INSERT INTO product (product_name, product_price, product_type, product_owner, image) VALUES ('".$productname."','".$productprice."','".$producttype."','".$product_owner."','".$image."')")
?>
MySQL数据库:
它只将我的图像路径上传到数据库。请帮我。我已经了解“图像”:_image.path,此代码是我的数据库保存图像路径而不是照片的结果。我已经在尝试使用路径位置进行搜索并将图像保存到 htdoc 文件,但在上传时不起作用。在 htdoc 中找不到该图像。请帮我。
解决方案
查看如何在 Flutter 中上传图片的答案?有关从颤振上传文件的更多信息。您将需要使用MultipartRequest
来自http库。
谷歌搜索还应该向您展示如何将文件保存到 MySQL,可能作为 blob。它本身就是一个完整的主题,您最终可能会将文件保存到文件系统并将路径存储在数据库中。
推荐阅读
- unity3d - Firebase 数据库检索用户的高分排名
- python - 如何在 Python 中更新字典中键字段的值?
- python - 在 Keras Sequential 模型中,Conv2D 似乎要求内核比前一层厚。为什么?
- matlab - 使用优化工具箱时如何解决“数组索引必须是正整数或逻辑值”?
- python-3.x - 从所有列中具有“无”值的数据框中删除行 - Python
- javascript - 路由器链接配置的目标不生成 html 代码
- arrays - 如何快速使用 Ms SQL 查询结果填充数组?
- c# - 当我使用 range.find 使用 vsto 搜索整个文档时,如何跳过表格?
- html - 如何将列表字典转换为html表?
- windows - 完成构建后如何从 appveyor 获取或下载构建的应用程序(exe 文件)