Flutter AZURE BLOB IMAGE UPLOAD - 如何将使用移动相机拍摄的图像上传到 azure blob 存储

标签 flutter dart azure-blob-storage

从昨天开始,我一直在努力尝试将使用移动相机从 iOS/Android 设备拍摄的图像上传到 azure blob 存储。

我可以上传文件,但由于某种原因它们已损坏无法打开上传的图像。

打开上传图片时请检查图片错误

Valid XHTML

我正在使用具有不同方法的 Flutter 包 http,所有这些都可以将图像文件上传到 azure blob 存储,但它会以某种方式损坏,我尝试将 ContentType 强制为 image/jpeg 但没有帮助。

这是我使用 http API 的代码 -

takePicture() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
        String fileName = basename(pickedFile.path);
        uploadFile(fileName, image);
      } else {
        print('No image selected.');
      }
    });
  }

第一种方法-->

http.Response response = await http.put(
      uri,
      headers: {
        "Content-Type": 'image/jpeg',
        "X-MS-BLOB-TYPE": "BlockBlob",
      },
      body: image.path,
    );
    print(response.statusCode);

使用第二个方法 -->

final data = image.readAsBytesSync();
  var dio = Dio();
  dio.options.headers['x-ms-blob-type'] = 'BlockBlob';
  dio.options.headers['Content-Type'] = 'image/jpeg';
  try {
    final response = await dio.put(
      '$url/$fileName?$token',
      data: data,
      onSendProgress: (int sent, int total) {
        if (total != -1) {
          print((sent / total * 100).toStringAsFixed(0) + "%");
        }
      },
    );
    print(response.statusCode);
  } catch (e) {
    print(e);
  }

接近第三个 -->

var request = new http.MultipartRequest("PUT", postUri);
      request.headers['X-MS-BLOB-TYPE'] = 'BlockBlob';
      request.headers['Content-Type'] = 'image/jpeg';
      request.files.add(
        new http.MultipartFile.fromBytes(
          'picture',
          await image.readAsBytes(),
        ),
      );
      request.send().then((response) {
        uploadResponse.add(response.statusCode);
      }, onError: (err) {
        print(err);
      });

非常感谢您的帮助。

最佳答案

如果你想在flutter应用中将图片上传到Azure Blob Storage,可以使用Dart包azblob来实现。包的使用方法请引用here .

例如

import 'package:image_picker/image_picker.dart';
import 'package:flutter/material.dart';
import 'package:azblob/azblob.dart';
import 'package:mime/mime.dart';

...
//use image_picker to get image

Future uploadImageToAzure(BuildContext context) async {
    try{
      String fileName = basename(_imageFile.path);
      // read file as Uint8List 
      Uint8List content =  await  _imageFile.readAsBytes();
      var storage = AzureStorage.parse('<storage account connection string>');
      String container="image";
      // get the mine type of the file
      String contentType= lookupMimeType(fileName);
      await storage.putBlob('/$container/$fileName',bodyBytes: content,contentType: contentType,type: BlobType.BlockBlob);
      print("done");
    } on AzureStorageException catch(ex){
      print(ex.message);
    }catch(err){
      print(err);
    }

enter image description here enter image description here

关于Flutter AZURE BLOB IMAGE UPLOAD - 如何将使用移动相机拍摄的图像上传到 azure blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65168151/

相关文章:

dart - 如何使用 TextPainter 绘制文本?

flutter - 'positions.isNotEmpty' : PageController. 在用它构建 PageView 之前无法访问页面。轮播 flutter

scala - 旋转 View 引擎外观相似

powershell - 使用 PowerShell(通过 Get-AzureStorageBlobContent)下载 Azure blob 非常慢,但通过 Azure Explorer 等下载速度很快?

ios - 如何从 Flutter 应用运行 ipa 文件

Flutter 使用 BloC 模式从 API 结果中获取列表元素

c# - 使用 C# 设置 Blob 的到期日期或在 Azure 存储中 X 天后自动删除 Blob?

Azure函数无法获取blob容器

flutter - 如何更改 FLUTTER DropdownMenuItem 的 textDirection(更改小部件方向)

Flutter:如何使用flutter_ffmpeg为视频添加水印和文字等叠加层?