我正在使用Nihilogic的"Canvas2Image"JavaScript工具将画布图转换为PNG图像.我现在需要的是使用PHP将这个工具生成的base64字符串转换为服务器上的实际PNG文件.
简而言之,我目前正在做的是使用Canvas2Image在客户端生成文件,然后检索base64编码的数据并使用AJAX将其发送到服务器:
// Generate the image file
var image = Canvas2Image.saveAsPNG(canvas, true);
image.id = "canvasimage";
canvas.parentNode.replaceChild(image, canvas);
var url = 'hidden.php',
data = $('#canvasimage').attr('src');
$.ajax({
type: "POST",
url: url,
dataType: 'text',
data: {
base64data : data
}
});
Run Code Online (Sandbox Code Playgroud)
此时,"hidden.php"收到一个看起来像数据的数据块:image/png; base64,iVBORw0KGgoAAAANSUhEUgAABE ...
从现在开始,我非常难过.从我读过的内容来看,我认为我应该使用PHP的imagecreatefromstring函数,但我不确定如何从base64编码的字符串中实际创建一个实际的PNG图像并将其存储在我的服务器上.请帮助!
我使用Web服务进行图像处理,它在Postman中运行良好:
现在我想用Dart扑灭http请求:
import 'package:http/http.dart' as http;
static ocr(File image) async {
var url = '${API_URL}ocr';
var bytes = image.readAsBytesSync();
var response = await http.post(
url,
headers:{ "Content-Type":"multipart/form-data" } ,
body: { "lang":"fas" , "image":bytes},
encoding: Encoding.getByName("utf-8")
);
return response.body;
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何上传图像文件,在上面的代码中我得到异常:Bad state: Cannot set the body fields of a Request with content-type "multipart/form-data".
我应该如何编写请求体?
我需要将一个PDF文件发布到一个远程REST API,我不能为我的生活找到它.无论我做什么,服务器都会响应我尚未将对象与file参数相关联.假设我有一个叫做PDF的文件test.pdf.这是我到目前为止所做的事情:
// Using an HttpClientRequest named req
req.headers.contentType = new ContentType('application', 'x-www-form-urlencoded');
StringBuffer sb = new StringBuffer();
String fileData = new File('Test.pdf').readAsStringSync();
sb.write('file=$fileData');
req.write(sb.toString());
return req.close();
Run Code Online (Sandbox Code Playgroud)
到目前为止,我已经尝试了几乎我write()对请求的数据的每个组合和编码,但无济于事.我尝试发送它codeUnits,我已经尝试使用a编码UTF8.encode,我尝试使用a编码它Latin1Codec,一切.我很难过.
任何帮助将不胜感激.
我看到了其他问题,但那不是我想要的,我不想将图像上传到服务器,我不想转换为base64 ...
我只想在表单数据或其他内容中发布文件并获取返回的信息.
我有这个,但没有工作:
void onTakePictureButtonPressed() {
takePicture().then((String filePath) {
if (mounted) {
setState(() {
imagePath = filePath;
videoController?.dispose();
videoController = null;
});
http.post('http://ip:8082/composer/predict', headers: {
"Content-type": "multipart/form-data",
}, body: {
"image": filePath,
}).then((response) {
print("Response status: ${response.statusCode}");
print("Response body: ${response.body}");
});
if (filePath != null) showInSnackBar('Picture saved to $filePath');
}
});
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 Flutter 插件 Image_picker 选择图像,以便在选择图像后上传图像
Future<File> _imageFile;
void _onImageButtonPressed(ImageSource source) async {
setState(() {
_imageFile = ImagePicker.pickImage(source: source);
});
}
Run Code Online (Sandbox Code Playgroud)
我在颤振文档中找到了这段代码,但它不起作用
var uri = Uri.parse("http://pub.dartlang.org/packages/create");
var request = new http.MultipartRequest("POST", url);
request.fields['user'] = 'nweiz@google.com';
request.files.add(new http.MultipartFile.fromFile(
'package',
new File('build/package.tar.gz'),
contentType: new MediaType('application', 'x-tar'));
request.send().then((response) {
if (response.statusCode == 200) print("Uploaded!");
});
Run Code Online (Sandbox Code Playgroud) dart ×4
flutter ×3
base64 ×1
dart-io ×1
form-data ×1
html5-canvas ×1
httprequest ×1
javascript ×1
php ×1
post ×1