我已经能够从我的电脑中选择一个文件并显示在我的Flutter Web应用程序中。我有一个函数(类型为File
),它获取一个文件并将其上传到服务器。比如functionName(File imageToSend)
。
但是当我试图把这个图像发送到服务器端时,它给了我一个错误。我正在使用下面的代码进行上传:
Uint8List uploadedImage;
File theChosenImg;
FileReader reader = FileReader();
FileReader reader2 = FileReader();
filePicker() async {
InputElement uploadInput = FileUploadInputElement();
uploadInput.click();
uploadInput.onChange.listen((e) {
// read file content as dataURL
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
reader.onLoadEnd.listen((e) {
setState(() {
uploadedImage = reader.result;
theChosenImg = files[0];
});
});
reader.readAsArrayBuffer(file);
reader2.readAsDataUrl(file);
}
});
}
字符串
当我使用变量uploadedImage
时,错误是Expected a value of type 'File', but got one of type 'String'
,然后我决定从theChosenImg = files[0];
使用theChosenImg
,这也告诉我数据类型不匹配。
是否可以将Uint8List
数据类型转换为File
?
更新代码
import 'dart:typed_data';
import 'dart:html';
import 'dart:ui' as ui;
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:web_image_upload/impUp.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class FrontUi extends StatefulWidget {
@override
_FrontUiState createState() => _FrontUiState();
}
class _FrontUiState extends State<FrontUi> {
Uint8List uploadedImage;
File theChosenImg;
String dispText = 'Uploaded image should shwo here.';
FileReader reader2 = FileReader();
_startFilePicker() async {
InputElement uploadInput = FileUploadInputElement();
uploadInput.click();
uploadInput.onChange.listen((e) {
// read file content as dataURL
final files = uploadInput.files;
if (files.length == 1) {
final file = files[0];
FileReader reader = FileReader();
reader.onLoadEnd.listen((e) {
setState(() {
uploadedImage = reader.result;
theChosenImg = files[0];
});
});
reader.readAsArrayBuffer(file);
reader2.readAsDataUrl(file);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView(
children: <Widget>[
Column(
children: <Widget>[
SizedBox(
height: 30,
),
Container(
height: 500,
width: 800,
child: Center(
child: uploadedImage == null
? Container(
child: Text(dispText),
)
: Container(
child: Image.memory(uploadedImage),
),
),
),
SizedBox(height: 20,),
CupertinoButton(
color: Colors.green,
child: Text("Choose"),
onPressed: (){
_startFilePicker();
},
),
SizedBox(height: 50,),
CupertinoButton(
color: Colors.green,
child: Text("Upload"),
onPressed: (){
PhotoCls().upload(reader2.result);
},
),
],
),
],
),
),
);
}
}
型
使用发送图像的方法上课
import 'dart:io';
import 'package:path/path.dart';
import 'package:async/async.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class PhotoCls {
String phpEndPoint = "http://IPv4 address/testlocalhost/uploadPicture.php";
upload(File imageFile) async {
// open a bytestream
var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
// get file length
var length = await imageFile.length();
// string to uri
var uri = Uri.parse(phpEndPoint);
// create multipart request
var request = new http.MultipartRequest("POST", uri);
// multipart that takes file
var multipartFile = new http.MultipartFile('file', stream, length,
filename: basename(imageFile.path));
// add file to multipart
request.files.add(multipartFile);
// send
var response = await request.send();
print(response.statusCode);
// listen for response
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});
}
}
型
4条答案
按热度按时间sirbozc51#
字符串
3htmauhk2#
包含此软件包https://pub.dev/packages/path_provider
字符串
neskvpey3#
我试图生成一个代码谁可以同时支持设备和网络在一起。
因为File.fromRawPath()使用了dart:io,它不适用于web。
这是我的解决方案:
字符串
首先,我用image_picker选择我的图像,然后用extended_image裁剪。
在我的代码中的某个地方裁剪后,我将裁剪的字节文件编码成jpg。
型
然后:
型
在我的例子中,我用Multer返回NodeJs来获取文件并保存它。
编辑:
型
更多帮助代码:
型
f45qwnt84#
使用Flutter用途:
字符串