在 Suave 应用程序中上传文件



我最近开始用F#和Suave为我大学的一个项目创建一个网站。我需要做的一件事是让用户通过网站上传文件,然后我将对其进行解析。

我现在拥有的是

let post =
   let ``process`` httpRequest =
      use reader = File.OpenText httpRequest.files.Head.tempFilePath
      // do parsing and save to database
   request ``process``

在我的 HTML 中,我有一个简单的表单

<form action="/parse" method="POST">
    <div>
        <input type="file" name="file">
    </div>
    <div>
        <button>Send</button>
    </div>
</form>

在通过post方法处理POST请求期间,我收到以下异常

[22:45:30 ERR] request failed
System.InvalidOperationException: The input list was empty.
  at Microsoft.FSharp.Collections.FSharpList`1[T].get_Head () <0x41494340 + 0x0007b> in <filename unknown>:0
  at Web+Parse+post@25.Invoke (Suave.HttpRequest httpRequest) <0x41493e10 + 0x0003b> in <filename unknown>:0
  at Microsoft.FSharp.Core.FSharpFunc`2[T,TResult].InvokeFast[V] (Microsoft.FSharp.Core.FSharpFunc`2 func, Microsoft.FSharp.Core.T arg1, Microsoft.FSharp.Core.TResult arg2) <0x4143c6a0 + 0x00099> in <filename unknown>:0
  at Web+Parse+post@39-4.Invoke (Suave.HttpContext context) <0x41493dc0 + 0x0002f> in <filename unknown>:0
  at Suave.WebPart+bind@14-5[a,b].Invoke (Microsoft.FSharp.Core.FSharpOption`1 _arg1) <0x41472a10 + 0x00044> in <filename unknown>:0
  at Microsoft.FSharp.Control.AsyncBuilderImpl+args@835-1[a,b].Invoke (a a) <0x41435c60 + 0x000af> in <filename unknown>:0

这意味着httpRequest.files列表中没有文件。

我找不到有关如何访问上传文件的任何信息,或者它是否已上传。我用Fiddler检查了我的浏览器发送的HTTP请求的内容,我只能看到

file=myFile.txt

那么文件是否正在上传?我是否正确访问了它?

注意:我使用的是 Suave 2.0.0

好的,我找到了我缺少的东西。要发布文件,表单必须具有如下所示的附加参数

<form action="/parse" method="POST" enctype="multipart/form-data">

这样,文件的内容将与"内容"部分中的 HTTP 请求一起发送。

(通过 https://searchcode.com/codesearch/view/7502482/发现)

最新更新