I have taken the the code form the question given below:
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.