我有一个服务器端代码,它接受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
未处理的例外:
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);
&amp; print(body.request)
我发送了一个包含内容的文本文件&#34; abcdef&#34;通过HTTP POST到服务器并获得以下响应:
打印(body.body);
--------------------------1e7b12f3c4249867
Content-Disposition: form-data; name="myfile"; filename="test.txt"
Content-Type: text/plain
abcdef
打印(body.request)
--------------------------1e7b12f3c4249867--
Instance of '_HttpRequest'
我也推荐了这篇文章:Dart Language: receive a file from a POST and print its contents on the server但在我的案例中没有帮助。
对此问题的任何帮助都会非常有帮助。
答案 0 :(得分:2)
此问题已通过包升级解决。问题在于包版本冲突。如果有人碰巧再次发现此问题,我强烈建议检查软件包版本并执行pub升级。