4

我正在摆弄用base64编码的PNG创建一个Blob......

  final FormData formData = new FormData();
  final String base64Image = "data:image/png;base64,iVBORw0K<reduced the data.....>gg==";

  final Blob blob = new Blob([base64Image],"image/png");

  formData.append('file', blob, "android.png");

  req.send(formData);

我不知道我做错了什么,但 blob 的内容是某种东西,而不是我想要的 png。

提前谢谢...

[更新]

  final FormData formData = new FormData();
  final String base64Image = "iVBORw0KGgo<...reduce data...>kJggg==";

  // BTW: I used the Base64 from dart-sdk/io/base64.dart
  final List<int> intList = Base64.decode(base64Image);
  final Int8Array int8array = new Int8Array.fromList(intList);
  final String atobString = window.atob(base64Image);

  // Does not work
  // final Blob blob = new Blob([atobString]);

  // The same...
  // final Blob blob = new Blob([int8array]);

  formData.append('file', blob, "android.png");
  //formData.append('new-filename', "icon-share.png");

  req.send(formData);

我认为 Base64.decode 生成的字节数还可以。文件大小为 1003 字节,解码也产生 1003 字节。

[更新 2 ] 这是我正在谈论的来源: https ://github.com/MikeMitterer/AndroidIconGenerator.DART/blob/master/test/src/restserver.dart

4

2 回答 2

1

好的,这是我自己的问题的答案:

import 'dart:convert'
...

test(' -> Upload File to REST-Server', () {

  final HttpRequest req = new HttpRequest();

  loadEnd(HttpRequest request) {
    if (request.readyState == HttpRequest.DONE) {

      switch(request.status) {

        case HttpStatus.HTTP_200_OK:
          expect(response['path'].endsWith("android.png"),true);

          break;

        case HttpStatus.HTTP_0_COMMUNICATION_FAILED:
          expect(request.status,HttpStatus.HTTP_200_OK);
          break;

        default:
          expect(request.status,HttpStatus.HTTP_200_OK);
          break;
      }
    }
  }

  req.open("POST", uriprovider.forUpload().toString());

  // REST returns JSON Data
  req.setRequestHeader('Accept', 'application/json');

  req.onLoadEnd.listen(expectAsync1((ProgressEvent e) => loadEnd(req)));

  final FormData formData = new FormData();

  final String base64Image = "data:image/png;base64,iVBORw0KG<code reduce for sample>RU5ErkJggg==";
  final String raw = "iVBORw0KG<code reduce for sample>RU5ErkJggg==";
  final String contenttype = "image/png";

  // Base64 is a modified version of dart-sdk/lib/io/base64.dart
  final List<int> intList = BASE64.decode(raw);
  final Int8Array int8array = new Int8Array.fromList(intList);

  // Converting to Uint8Array brought the solution!
  final Uint8Array uint8array = new Uint8Array(intList.length);

  // does not work!
  //var binary = window.atob(raw);

  final Blob blob = new Blob([uint8array]);

  formData.append('file', blob, "android.png");

  req.send(formData);

});

感谢大家把我推向正确的方向!

于 2013-02-08T19:17:44.840 回答
0

在编码为 Blob 之前,需要解码 base64 (即 atob)url。这很简单!试试看!

于 2013-02-04T09:43:30.090 回答