使用正确名称的Suave发送(zip)文件

JvS*_*JvS 3 f# suave

我一直在研究使用Suave创建Web服务器。目前,我正在尝试让它在GET请求中发送一个zip文件。我已经成功地使我的应用程序发送了文件,但是在Postman中执行请求时收到的文件名是“ response”或“ response.html”,具体取决于Files我使用的Suave 模块中的哪个函数。就是说,当我手动将文件重命名为.zip时,可以像正常的.zip文件一样打开和解压缩文件,这意味着问题确实出在下载文件的名称上。下面的代码是我现在得到的。

open JSON
open System
open System.Threading
open Suave
open Suave.Filters
open Suave.Operators
open Suave.Successful
open Suave.Web
open System.IO
open Suave.Writers

let sampleJsonPath = @"C:\VS Projects\Research\Sample.json"
let sampleZipPath = @"C:\VS Projects\Research\Sample.zip"
let getJson() =
    File.ReadAllText(sampleJsonPath)

[<EntryPoint>]
let main argv = 
    let cts = new CancellationTokenSource()
    let mimeTypes =
        defaultMimeTypesMap
            @@ (function | ".zip" -> createMimeType "compression/zip" false | _ -> None)

    let config =
        { defaultConfig with 
            mimeTypesMap = mimeTypes
            cancellationToken = cts.Token
        }

    let app = 
        choose 
            [ GET >=> choose
                [ path "/hello" >=> OK "Hello GET"
                path "/jsonString" >=> OK (getJson())
                path "/jsonFile" >=> warbler (fun _ -> getJson() |> JSON)// for only on startup: ... >=> (getJson() |> JSON)
                path "/zip" >=> Files.sendFile sampleZipPath false
                path "/goodbye" >=> OK "Goodbye GET" ]
            POST >=> choose
                [ path "/hello" >=> OK "Hello POST"
                pathScan "/content/%d" (fun param -> OK (sprintf "Found integer:\n%d" param))
                pathScan "/content/%s" (fun param -> OK (sprintf "Found content:\n%s" param))
                path "/goodbye">=> OK "Goodbye POST" ]
            RequestErrors.BAD_REQUEST "Unknown request encountered"
            ]
    let listening, server = startWebServerAsync config app

    Async.Start(server, cts.Token)
    printfn "Ready to receive requests"
    Console.ReadKey true |> ignore

    cts.Cancel()

    0 // return an integer exit code
Run Code Online (Sandbox Code Playgroud)

到目前为止,谷歌搜索还没有发现我可以使用的东西。我还尝试了Suave Files模块中的许多其他功能,包括Files.browseFile sampleZipPath "Sample.zip"(它也提供了一个名为“ response.html”的文件)和Files.file sampleZipPath(它提供了一个名为“ response”的文件),但到目前为止没有成功。

如何提供要发送的文件名?

Art*_*lev 5

文件名是使用HTTP响应标头“ Content-Disposition”设置的,并且不会由Suave自动处理:

   let setFileName name =
     setHeader  "Content-Disposition" (sprintf "inline; filename=\"%s\"" name)
Run Code Online (Sandbox Code Playgroud)

因此,代码将为

path "/zip" >=> setFileName "Sample.zip"
            >=> Files.sendFile sampleZipPath false
Run Code Online (Sandbox Code Playgroud)

根据所需behaviur可以取代inline;与部分attachment;在浏览器中显示“保存文件”对话框