如何将Flutter中拍摄的照片发送到.NET Web服务器?

时间:2020-11-04 09:43:44

标签: vb.net flutter http-post

我正在尝试将抖动捕获的图片发送到Web服务器,并将其保存到getTemporaryDirectory()中。我尝试将它们作为ByteStream发送,但是当我尝试将ByteStream转换为图像时,出现异常“参数无效”。 exception.StackTrace表示以下内容:

在System.Drawing.Image.FromStream(流流,布尔值useEmbeddedColorManagement,布尔值validateImageData) 在System.Drawing.Image.FromStream(Stream stream)

这就是我从异常中获得的所有信息。

我的代码如下:

颤振

       final request = http.MultipartRequest("POST", Uri.parse(url));

    List<FileSystemEntity> pictures = new List();
    Directory directory = await getTemporaryDirectory();
    pictures = directory.listSync();
    print(pictures);

    for (FileSystemEntity picture in pictures) {
      if (picture is File) {
        File file = picture;
        String fileName = file.path.split('/').last;
        var stream = new http.ByteStream(StreamView(file.openRead()));
        var length = await file.length();
        var multipartFileSign =
            new http.MultipartFile('photo', stream, length, filename: fileName);
        request.files.add(multipartFileSign);
      }
    }

    String credentials = user.userName + ":" + user.passwordhash;
    Codec<String, String> stringToBase64 = utf8.fuse(base64);
    String encodedcredentials = stringToBase64.encode(credentials);

    Map<String, String> headers = {
      HttpHeaders.acceptHeader: "application/json",
      HttpHeaders.contentTypeHeader: "application/json; charset=UTF-8",
      "device": settings.deviceName,
      "Authorization": "Basic " + encodedcredentials,
      "token": gBenutzer.token
    };

    request.headers.addAll(headers);

    var response = await request.send();
    print(response);

  }

.NET

<HttpPost()>
       <Route("url")>
       Public Function PostPictures() As IHttpActionResult

           Dim result As Byte() = Request.Content.ReadAsByteArrayAsync().Result
           Dim res As Stream = Request.Content.ReadAsStreamAsync().Result

           Try
               'I've tried all 3 methods below here to convert the ByteArray/Stream to an Image, all with the same exception.
                
               #1 
               Dim stream = New MemoryStream(result)
               Dim image As Image = Image.FromStream(stream)

                #2
                Dim image As Image = Image.FromStream(res)              

                #3
               Dim converter As New ImageConverter
               Dim img As Image = CType(converter.ConvertFrom(result), Image)


           Catch ex As Exception
               Debug.WriteLine(ex.Message)

           End Try





       End Function

我想念什么吗?提前非常感谢!

0 个答案:

没有答案
相关问题