Dart:从 Dart 服务器上的 HTTP POST 请求接收文件,并在服务器上 save/print 其内容

Dart: receive a file from a HTTP POST request on Dart server and save/print its contents on the server

我有一个服务器端代码,它接受来自 POST 请求的文件并将内容复制到一个新文件。我基本上是在尝试重现 Dart how to upload image

中提到的示例
import 'dart:io';
import 'packages/http_server/http_server.dart';

void main() 
{
 HttpServer.bind('127.0.0.1', 8081)
  .then((HttpServer server) {
  server.listen((HttpRequest request) {
  server.listen((HttpRequest request) {
    if (request.method.toLowerCase() == 'post') {
    HttpBodyHandler.processRequest(request).then((body) {
      HttpBodyFileUpload fileUploaded = body.body['myfile'];
      final file = new File('abc.txt');
      file.writeAsBytes(fileUploaded.content, mode: FileMode.WRITE)
          .then((_) {
        request.response.close();
      });
    });
   } 
}); 
});
}

当我使用 curl 向服务器发送文件时出现以下错误 post:

curl -v -F myfile=@C:/aaa.txt -H "Content-Type: text/plain" http://localhost8081

Unhandled exception:

type 'String' is not a subtype of type 'int' of 'index'.
#0      _StringBase.[] (dart:core-patch/string_patch.dart:231)
#1      main.<anonymous closure>.<anonymous closure>.<anonymous closure>  (file:///C:/Dart%20Examples/dart-samples-master/dart-samples-master/html5/web/file/dndfiles/uploadServer.dart:10:54)
#2      _RootZone.runUnary (dart:async/zone.dart:1149)
#3      _Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:551)
#4      _Future._propagateToListeners (dart:async/future_impl.dart:637)
#5      _Future._complete (dart:async/future_impl.dart:414)
#6      Stream.fold.<anonymous closure> (dart:async/stream.dart:624)
#7      _RootZone.runGuarded (dart:async/zone.dart:1076)
#8      _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:390)
#9      _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:399)
#10     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:290)
#11     _SinkTransformerStreamSubscription._close (dart:async/stream_transformers.dart:95)
#12     _EventSinkWrapper.close (dart:async/stream_transformers.dart:18)
#13     _StringAdapterSink.close (dart:convert/string_conversion.dart:267)
#14     _Utf8ConversionSink.close (dart:convert/string_conversion.dart:324)
#15     _ConverterStreamEventSink.close (dart:convert/chunked_conversion.dart:84)
#16     _SinkTransformerStreamSubscription._handleDone (dart:async/stream_transformers.dart:140)
#17     _RootZone.runGuarded (dart:async/zone.dart:1076)
#18     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:390)
#19     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:399)
#20     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:290)
#21     _ForwardingStream._handleDone (dart:async/stream_pipe.dart:112)
#22     _ForwardingStreamSubscription._handleDone (dart:async/stream_pipe.dart:180)
#23     _RootZone.runGuarded (dart:async/zone.dart:1076)
#24     _BufferingStreamSubscription._sendDone.sendDone (dart:async/stream_impl.dart:390)
#25     _BufferingStreamSubscription._sendDone (dart:async/stream_impl.dart:399)
#26     _BufferingStreamSubscription._close (dart:async/stream_impl.dart:290)
#27     _StreamController&&_SyncStreamControllerDispatch._sendDone (dart:async/stream_controller.dart:752)
#28     _StreamController._closeUnchecked (dart:async/stream_controller.dart:605)
#29     _StreamController.close (dart:async/stream_controller.dart:598)
#30     _HttpParser._closeIncoming (dart:io/http_parser.dart:1046)
#31     _HttpParser._doParse (dart:io/http_parser.dart:784)
#32     _HttpParser._parse (dart:io/http_parser.dart:330)
#33     _HttpParser._onData (dart:io/http_parser.dart:819)
#34     _RootZone.runUnaryGuarded (dart:async/zone.dart:1087)
#35     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#36     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:270)
#37     _StreamController&&_SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:744)
#38     _StreamController._add (dart:async/stream_controller.dart:616)
#39     _StreamController.add (dart:async/stream_controller.dart:562)
#40     _Socket._onData (dart:io-patch/socket_patch.dart:1646)
#41     _RootZone.runUnaryGuarded (dart:async/zone.dart:1087)
#42     _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#43     _BufferingStreamSubscription._add (dart:async/stream_impl.dart:270)
#44     _StreamController&&_SyncStreamControllerDispatch._sendData (dart:async/stream_controller.dart:744)
#45     _StreamController._add (dart:async/stream_controller.dart:616)
#46     _StreamController.add (dart:async/stream_controller.dart:562)
#47     _RawSocket._RawSocket.<anonymous closure> (dart:io-patch/socket_patch.dart:1215)
#48     _NativeSocket.issueReadEvent.issue (dart:io-patch/socket_patch.dart:749)
#49     _microtaskLoop (dart:async/schedule_microtask.dart:41)
#50     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
#51     _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:96)
#52     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:149)

编辑:

我将上面代码中的 HttpBodyFileUpload fileUploaded = body.body['myfile']; 更改为 print(body.body); & print(body.request) 并且我通过 HTTP POST 向服务器发送了一个内容为 "abcdef" 的文本文件并得到以下回复:

打印(body.body);

--------------------------1e7b12f3c4249867
Content-Disposition: form-data; name="myfile"; filename="test.txt"
Content-Type: text/plain

abcdef

打印(body.request)

--------------------------1e7b12f3c4249867--

Instance of '_HttpRequest'

我也提到了 post:Dart Language: receive a file from a POST and print its contents on the server 但它对我的情况没有帮助。

有关此问题的任何帮助都会非常有帮助。

此问题已通过软件包升级得到解决。问题在于包版本冲突。如果有人碰巧再次遇到这个问题,我强烈建议检查包版本并执行发布升级。