上传 Image/File 到 Strapi (Flutter Web)
Upload Image/File to Strapi (Flutter Web)
我正在尝试通过 Flutter Web 将图像上传到 Strapi。我知道(从这个 link) that I need to use FormData to do so. I've researched on many ways to do this and I stumble across Dio and of course Http.
两种解决方案都给我错误:
Unsupported operation: MultipartFile is only supported where dart:io is available.
我试过这个代码:
var request = new http.MultipartRequest("POST", Uri.parse(url));
request.files.add(
await http.MultipartFile.fromPath(
"files",
imageFilePath,
),
);
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
print(response.statusCode);
}).catchError((e) => print(e));
按照建议here。
当我使用 MultipartFile.fromBytes(...)
.
时,还有许多其他错误或空数据 (400)
我只是想上传一个文件,因此我假设我的正文应该只包含 files
的 FormData,正如 Strapi's Documentation.
中提到的那样
所以,我在 Flutter Discord 上搜索了一些帮助,我发现我的问题是因为 Flutter Web 无法使用 'dart:io'
,而使用 'dart:html'
会取消所有的使用Flutter 平台的数量。
我最终使用了这个导入:
import 'dart:convert';
import 'dart:typed_data';
import 'package:image_picker/image_picker.dart';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart';
import 'package:async/async.dart';
这是我创建并运行的函数:
Future<bool> uploadImage(
String imageFilePath,
Uint8List imageBytes,
) async {
String url = SERVERURL + "/uploadRoute";
PickedFile imageFile = PickedFile(imageFilePath);
var stream =
new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var uri = Uri.parse(url);
int length = imageBytes.length;
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile('files', stream, length,
filename: basename(imageFile.path),
contentType: MediaType('image', 'png'));
request.files.add(multipartFile);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
我在使用 Strapi 时遇到了这个问题,这个解决方案非常有效。
在使用 Flutter 创建条目期间上传文件
dio: ^3.0.10
、mime: ^1.0.0
和 http_parser
.
如果是foo
,主要部分是文件的键名,所以你必须把files.image
改成files.foo
才能上传
final mimeType = mime.lookupMimeType(imageFile.path, headerBytes: [0xFF, 0xD8])?.split('/');
FormData formData = new FormData.fromMap(
{
"files.image": await MultipartFile.fromFile(
imageFile.path,
filename: imageFile.path.split('/').last,
contentType: MediaType(mimeType?[0], mimeType?[1]),
),
"data": jsonEncode({
"title": title,
"summary": summary,
"content": content,
}),
},
);
Response response = await _dio.post(
"/blogs",
data: formData,
options: Options(),
);
在 Flutter Web 中:
我正在使用 node.js 和 express 作为后端源 我创建了以下简单方法来上传 图像和文本数据 到 localhost 服务器使用xamp
String url = 'http://localhost:8080/blog_upload';
var request = http.MultipartRequest('POST', Uri.parse(url));
imagebytes = await image.readAsBytes();
List<int> listData = imagebytes!.cast();
request.files.add(http.MultipartFile.fromBytes('field_name', listData ,
filename:'myFile.jpg'));
request.fields['name'] = 'Mehran Ullah Khan';
request.fields['country'] = 'Pakistan';
var response = await request.send();
我在上面的方法中使用了如下依赖
http: ^0.13.3
image_picker: ^0.8.2
我们如何使用image_picker?
首先全局声明 image
和 imagebytes
image = await ImagePicker().pickImage(source: ImageSource.gallery);
这是我在后端使用的方法。
app.post('/blog_upload', upload.single('field_name'), async (req, res, next) => {
let data = {name_db: req.body['name'], Country_db:req.body['country'],};
let sqlq = `INSERT INTO TableName SET ? `;
db.query(sqlq, data, (insertionErr, insertionResult) => {
if (insertionErr) {
console.log('failed_upload');
throw insertionErr;
}
else {
console.log('done_upload');
res.send(insertionResult);
}
});
});
上面的方法我都用过
multer and express
包
您可以在此 link
中查看有关 multer 和 express 的完整详细信息
@Diego Cattarinich Clavel
字符串url = Constants.BASE_URL + HttpUrls.categories;
PickedFile imageFile = PickedFile(路径);
变量流 =
新 http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var uri = Uri.parse(url);
int length = imageBytes.length;
var request = new http.MultipartRequest(
"PUT",
uri,
);
var multipartFile = new http.MultipartFile(
'files.categoryImage',
stream,
length,
filename: basename(imageFile.path),
contentType: MediaType('image', 'png'),
);
request.files.add(multipartFile);
request.fields['data'] = '{"category":$category}';
print(request.url);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
我正在尝试通过 Flutter Web 将图像上传到 Strapi。我知道(从这个 link) that I need to use FormData to do so. I've researched on many ways to do this and I stumble across Dio and of course Http.
两种解决方案都给我错误:
Unsupported operation: MultipartFile is only supported where dart:io is available.
我试过这个代码:
var request = new http.MultipartRequest("POST", Uri.parse(url));
request.files.add(
await http.MultipartFile.fromPath(
"files",
imageFilePath,
),
);
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
print(response.statusCode);
}).catchError((e) => print(e));
按照建议here。
当我使用 MultipartFile.fromBytes(...)
.
我只是想上传一个文件,因此我假设我的正文应该只包含 files
的 FormData,正如 Strapi's Documentation.
所以,我在 Flutter Discord 上搜索了一些帮助,我发现我的问题是因为 Flutter Web 无法使用 'dart:io'
,而使用 'dart:html'
会取消所有的使用Flutter 平台的数量。
我最终使用了这个导入:
import 'dart:convert';
import 'dart:typed_data';
import 'package:image_picker/image_picker.dart';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart';
import 'package:async/async.dart';
这是我创建并运行的函数:
Future<bool> uploadImage(
String imageFilePath,
Uint8List imageBytes,
) async {
String url = SERVERURL + "/uploadRoute";
PickedFile imageFile = PickedFile(imageFilePath);
var stream =
new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var uri = Uri.parse(url);
int length = imageBytes.length;
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile('files', stream, length,
filename: basename(imageFile.path),
contentType: MediaType('image', 'png'));
request.files.add(multipartFile);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
我在使用 Strapi 时遇到了这个问题,这个解决方案非常有效。
在使用 Flutter 创建条目期间上传文件
dio: ^3.0.10
、mime: ^1.0.0
和 http_parser
.
如果是foo
,主要部分是文件的键名,所以你必须把files.image
改成files.foo
才能上传
final mimeType = mime.lookupMimeType(imageFile.path, headerBytes: [0xFF, 0xD8])?.split('/');
FormData formData = new FormData.fromMap(
{
"files.image": await MultipartFile.fromFile(
imageFile.path,
filename: imageFile.path.split('/').last,
contentType: MediaType(mimeType?[0], mimeType?[1]),
),
"data": jsonEncode({
"title": title,
"summary": summary,
"content": content,
}),
},
);
Response response = await _dio.post(
"/blogs",
data: formData,
options: Options(),
);
在 Flutter Web 中:
我正在使用 node.js 和 express 作为后端源 我创建了以下简单方法来上传 图像和文本数据 到 localhost 服务器使用xamp
String url = 'http://localhost:8080/blog_upload';
var request = http.MultipartRequest('POST', Uri.parse(url));
imagebytes = await image.readAsBytes();
List<int> listData = imagebytes!.cast();
request.files.add(http.MultipartFile.fromBytes('field_name', listData ,
filename:'myFile.jpg'));
request.fields['name'] = 'Mehran Ullah Khan';
request.fields['country'] = 'Pakistan';
var response = await request.send();
我在上面的方法中使用了如下依赖
http: ^0.13.3
image_picker: ^0.8.2
我们如何使用image_picker?
首先全局声明 image
和 imagebytes
image = await ImagePicker().pickImage(source: ImageSource.gallery);
这是我在后端使用的方法。
app.post('/blog_upload', upload.single('field_name'), async (req, res, next) => {
let data = {name_db: req.body['name'], Country_db:req.body['country'],};
let sqlq = `INSERT INTO TableName SET ? `;
db.query(sqlq, data, (insertionErr, insertionResult) => {
if (insertionErr) {
console.log('failed_upload');
throw insertionErr;
}
else {
console.log('done_upload');
res.send(insertionResult);
}
});
});
上面的方法我都用过
multer and express
包
您可以在此 link
@Diego Cattarinich Clavel
字符串url = Constants.BASE_URL + HttpUrls.categories; PickedFile imageFile = PickedFile(路径); 变量流 = 新 http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
var uri = Uri.parse(url);
int length = imageBytes.length;
var request = new http.MultipartRequest(
"PUT",
uri,
);
var multipartFile = new http.MultipartFile(
'files.categoryImage',
stream,
length,
filename: basename(imageFile.path),
contentType: MediaType('image', 'png'),
);
request.files.add(multipartFile);
request.fields['data'] = '{"category":$category}';
print(request.url);
var response = await request.send();
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});