0

I have a server side code which accepts a file from POST request and copy the contents to a new file. I am basically trying to reproduce the example mentioned on 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();
      });
    });
   } 
}); 
});
}

I get the below error when I send a file to the server using 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)

EDIT:

I changed HttpBodyFileUpload fileUploaded = body.body['myfile']; in above code to print(body.body); & print(body.request) and I sent a text file with content "abcdef" to the server via HTTP POST and got the below response:

print(body.body);

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

abcdef

print(body.request)

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

Instance of '_HttpRequest'

I have referred the post as well: Dart Language: receive a file from a POST and print its contents on the server but it is not helping in my case.

Any assistance on this issue will be very helpful.

Community
  • 1
  • 1
Rahul
  • 21
  • 5
  • The stacktrace should point to the exact source location where this exception is thrown. Can you please provide this information. Doesn't seem to be part of the code in your question. – Günter Zöchbauer Feb 24 '16 at 16:37
  • I can't get where `uploadServer.dart:12:54` exactly is. Can you give some indication what code line (12) that is in your question? – Günter Zöchbauer Feb 24 '16 at 16:47
  • @GünterZöchbauer I update the stacktrace in the question. Please have a look, thank you. – Rahul Feb 24 '16 at 16:47
  • @GünterZöchbauer Just updated the question, uploadServer.dart is the file name of the server. – Rahul Feb 24 '16 at 16:55
  • Can you please change `HttpBodyFileUpload fileUploaded = body.body['myfile'];` to `print(body.body)` and alternatively `print(body.request)` and add the output to your question? – Günter Zöchbauer Feb 24 '16 at 17:03
  • @GünterZöchbauer I updated the question with change and the output. Please check, thank you. – Rahul Feb 24 '16 at 17:43
  • Could you publish a full example to a GitHub repo so I can try it out locally without rebuilding a whole project? – Günter Zöchbauer Feb 24 '16 at 17:47
  • 1
    This issue was resolved with package upgrade. There was some issue with the package version. – Rahul Jul 14 '16 at 21:59

1 Answers1

2

This issue was resolved with package upgrade. The problem was with conflicting package versions. If anyone happens to stumble upon this issue again I would highly suggest checking the package versions and performing a pub upgrade.

Rahul
  • 21
  • 5