【问题标题】:How to upload a file from input type file in dart?如何从 dart 中的输入类型文件上传文件?
【发布时间】:2013-01-30 16:36:39
【问题描述】:

经过大量搜索后,我无法在通过 dart 上传时反序列化简单的文本文件。

我知道这可能会引发很多反对票,但是如何在 dart 中上传文件的简单演示会有所帮助?

无论是在控制台中还是在 dart 中的 Web 应用程序中。我只想上传一个包含一些基本单词的文本文件。

【问题讨论】:

    标签: file-upload dart


    【解决方案1】:

    以下简单示例对我有用:

    在编辑器中使用以下文件结构:

    fileuploadtest/
      fileupload.dart
      index.html
    

    index.html(注意:这里没有 Dart!)

    <!DOCTYPE html>
    
    <html>
      <head>
        <title>index</title>
      </head>
    
      <body>   
        <form enctype="multipart/form-data" action="foo" method="POST">
          <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
            Choose a file to upload: <input name="uploadedfile" type="file" /><br />
          <input type="submit" value="Upload File" />
        </form>   
      </body>
    </html>
    

    fileupload.dart

    这将创建一个静态文件处理程序(为简单起见)始终为 index.html 服务以响应任何 GET 请求,以及一个文件上传处理程序响应任何 POST 请求并打印出上传文件的内容(以及,实际上,整个 POST 数据 - 您必须提取相关位)。

    import 'dart:io';
    
    void main() {
      var httpServer = new HttpServer();
    
      // attach handlers:
    
      var static = new StaticFileHandler();
      httpServer.addRequestHandler(static.matcher, static.handler);
    
      var fileUploadHandler = new FileUploadHandler();
      httpServer.addRequestHandler(fileUploadHandler.matcher, 
          fileUploadHandler.handler);
    
      // start listening
      httpServer.listen("127.0.0.1", 8081);
    }
    
    class FileUploadHandler {
      bool matcher(req) => req.method == "POST"; // return true if method is POST
    
      void handler(req,res) {
        req.inputStream.onData = () {
          var data = req.inputStream.read();
          var content = new String.fromCharCodes(data);
          print(content); // print the file content.
        };
      }
    }
    
    class StaticFileHandler {
      //  return true for all GET requests.
      bool matcher(req) {
        print("Path: ${req.path}");
        return req.method=="GET"; 
      }
    
      void handler(req,res) {
        var file = new File("index.html"); // only serve index.html in the same folder
        file.openInputStream().pipe(res.outputStream);
      }
    }
    

    启动fileUpload.dart 并使用浏览器导航到http://localhost:8081/index.html

    对于名为foo.txt 的文件,包含以下内容:

    foo
    bar
    

    这是我得到的全部日志输出(从 Dart 编辑器的控制台发布)

    ------WebKitFormBoundaryw7XBqLKuA7nP1sKc
    
    Content-Disposition: form-data; name="MAX_FILE_SIZE"
    
    100000
    
    ------WebKitFormBoundaryw7XBqLKuA7nP1sKc
    
    Content-Disposition: form-data; name="uploadedfile"; filename="foo.txt"
    
    Content-Type: text/plain
    
    foo
    bar
    
    ------WebKitFormBoundaryw7XBqLKuA7nP1sKc--
    

    【讨论】:

    • 不错的答案,但我仍然想知道我们现在是否应该通过 AJAX/WS 上传文件而不是旧表单的东西 ;) ...您知道,要能够取消正在进行的上传,请查看进度等。
    • 公平点。我试图为服务器端找到最简单的答案。
    • 确实如此。想写一个文件上传包有一段时间了,一直没有时间。让我们看看这个周末是否有所不同。
    猜你喜欢
    • 2014-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多