0

I have taken the the code form the question given below:

How to upload a file in Dart?

The Client Code

void main(){
     InputElement uploadInput = query('#uploadFile');
     uploadInput.on.change.add((e) {
     // read file content as dataURL
     final files = uploadInput.files;
     if (files.length == 1) {
              final file = files[0];
              final reader = new FileReader();
              reader.on.load.add((e) {
                     sendFile(reader.result);
              });
              reader.readAsText(file);
     }
  }); 
}

sendFile(dynamic data) {
    final req = new HttpRequest();
    req.on.readyStateChange.add((Event e) {
      if (req.readyState == HttpRequest.DONE &&
         (req.status == 200 || req.status == 0)) {
         window.alert("test successful");
      }
    });
    req.open("POST", "http://127.0.0.1:8080/upload");
    req.send(data);
    print(req.response);
}

The Server code with minute changes as given below:

void UploadFile(HttpRequest request, HttpResponse response) {
     //some logic
     _readBody(request, (body) {
       var logFile = new File('test.txt');
       var out = logFile.openOutputStream(FileMode.WRITE);
       out.writeString(body);
       out.close();
       response.statusCode = HttpStatus.CREATED;
       response.contentLength = 0;
       response.outputStream.close();
     });
}

_readBody(HttpRequest request, void handleContent(String body)) {
     String contentString = ""; // request body byte data
     final completer = new Completer();
     final textFile = new StringInputStream(request.inputStream);
     textFile.onData = (){
              print("inside data");
              contentString = contentString.concat(textFile.read());
     };
     textFile.onClosed = () {
           completer.complete("");
     };
     textFile.onError = (Exception e) {
           print('exeption occured : ${e.toString()}');
     };

    // process the request and send a response
    completer.future.then((_){
           handleContent(contentString);
    });
   }

I dont know what is the error since I am unable to debug on console app in windows in dart editor. But I am not able to understand what is the error since My test file consist of no text that I sent from the uploaded file.

Community
  • 1
  • 1
IBRIT
  • 385
  • 1
  • 3
  • 13
  • I've not run the code, but it might be worth adding a few `print("...")` statements into it to help debugging (for example, is the line `completer.complete("")` ever being called, and the `handleContent(contentString)` call - you could `print(contentString);` first, to output the data you're hoping to save to the file. Also, the latest trunk version of the Dart editor supports server-side debugging on windows. – Chris Buckett Jan 24 '13 at 20:14
  • I am updating the dart editor and then will debug again.I don't see debugger updates on changelog for console app in windows though.Will let you know once I update and debug. – IBRIT Jan 24 '13 at 21:27
  • I'm server-side debugging on my win7 box with build 17463 (and I'm sure it's been working for at least a couple of weeks). BTW, I've run the "file save" bit of code (from `var logfile` to `out.close()` from your snippet above, and that saves correctly, so it looks like you're not retrieving the body content to pass to the file. – Chris Buckett Jan 24 '13 at 22:04

0 Answers0