无法获取上传文件中的内容?

时间:2013-01-24 17:13:09

标签: file-upload dart

我已经从以下问题中获取了代码形式:

How to upload a file in Dart?

客户代码

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);
}

具有微小更改的服务器代码,如下所示:

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);
    });
   }

我不知道是什么错误,因为我无法在dart编辑器的Windows中调试控制台应用程序。 但我无法理解错误是什么,因为我的测试文件不包含我从上传文件发送的文本。

0 个答案:

没有答案