[英]How to get file stream value from "file_picker" flutter web?
我需要從圖庫中選擇一張圖片,還有一個用於拖動圖片的字段。 對於拖放字段,我使用了 flutter_dropzone 。 並使用getFileStream(event)
數據將數據上傳到file_picker: ^5.2.4
用於從圖庫中選擇圖像。那么如何從這個 package 獲取文件流數據。我得到了字節,但這不起作用我需要文件流值
使用 file_picker 的代碼
void chooseImage() async {
pickedFile = await FilePicker.platform.pickFiles(
type: FileType.custom,
withReadStream: true,
allowedExtensions: [
'jpg','jpeg','png'
]
);
if (pickedFile != null) {
try {
base64 = pickedFile!.files.first.bytes;
base64String.value=base64.toString();
String mime = pickedFile!.files.first.extension.toString();
getS3url("image/$mime" ,base64String.value,from: "cameraIcon");
//withReadStream
//getFileStream(event);
} catch (err) {
print(err);
}
} else {
}
}
復制自https://github.com/miguelpruivo/flutter_file_picker/wiki/FAQ
import 'package:file_picker/file_picker.dart';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';
void main() async {
final result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: [
'jpg',
'png',
'mp4',
'webm',
],
withData: false,
withReadStream: true,
);
if (result == null || result.files.isEmpty) {
throw Exception('No files picked or file picker was canceled');
}
final file = result.files.first;
final filePath = file.path;
final mimeType = filePath != null ? lookupMimeType(filePath) : null;
final contentType = mimeType != null ? MediaType.parse(mimeType) : null;
final fileReadStream = file.readStream;
if (fileReadStream == null) {
throw Exception('Cannot read file from null stream');
}
final stream = http.ByteStream(fileReadStream);
final uri = Uri.https('siasky.net', '/skynet/skyfile');
final request = http.MultipartRequest('POST', uri);
final multipartFile = http.MultipartFile(
'file',
stream,
file.size,
filename: file.name,
contentType: contentType,
);
request.files.add(multipartFile);
final httpClient = http.Client();
final response = await httpClient.send(request);
if (response.statusCode != 200) {
throw Exception('HTTP ${response.statusCode}');
}
final body = await response.stream.transform(utf8.decoder).join();
print(body);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.