jonataslaw / dart-server-nano

A light, fast, and friendly server written in Dart.
MIT License
23 stars 4 forks source link

Unable to upload txt file #17

Open X-Shao opened 1 month ago

X-Shao commented 1 month ago
 server.post("/upload", (req, res) async {
      try {
        if (req.isMultipart) {
          final file = await req.payload();
          MultipartUpload upload = file!["file"];
          String filePath = p.join(uploadPath, upload.name);
          await upload.toFile(filePath);
          return res.sendJson({
            "code": 0,
            "message": "ok",
          });
        } else {
          return res.sendJson({
            "code": -1,
            "message": "ok",
          });
        }
      } catch (e) {
        print("uploadDatabase : $e");
        return res.sendJson({
          "code": -1,
          "message": "ok",
        });
      }
    });
When I upload txt, the payload cannot return the result normally, and the connection will be terminated abnormally. Uploading other files will not have this problem
j787701730 commented 1 month ago

Maybe I also have the same problem, But I have an error message

  server.post('/test', (req, res) async {
    final file = await req.payload();
    res.send("string");
  });
type 'String' is not a subtype of type 'Iterable<int>'
#0      ContextRequest.payload.<anonymous closure> (package:server_nano/src/context/context_request.dart:136:24)
<asynchronous suspension>
<input
          type="file"
          onChange={(e) => {
            console.log(e.target.files);
            const formData = new FormData();
            formData.append('type', 'this.state.value');
            formData.append('title', 'this.state.title');
            formData.append('file', e.target.files[0]);
            axios(pathAddApiString('test'), {
              method: 'POST',
              headers: {
                'Content-Type': 'multipart/form-data',
              },
              data: formData,
            }).then((res) => {});
          }}
        />