GithubHelp home page GithubHelp logo

File upload about giraffe HOT 4 CLOSED

nojaf avatar nojaf commented on July 4, 2024 1
File upload

from giraffe.

Comments (4)

dustinmoris avatar dustinmoris commented on July 4, 2024 2

Hi,

I spent some time looking into this and I created an example in the SampleApp. I have added two new HTTP POST endpoints, one for small file uploads and another for large file uploads:

POST >=>
    choose [
        route "/small-upload" >=> smallFileUploadHandler
        route "/large-upload" >=> largeFileUploadHandler ]

Then I implemented two handlers to deal with small and large file uploads:

open System.Threading
open Microsoft.AspNetCore.Http.Features

let smallFileUploadHandler =
    fun (ctx : HttpContext) ->
        async {
            return!
                (match ctx.Request.HasFormContentType with
                | false -> setStatusCode 400 >=> text "Bad request"
                | true  ->
                    ctx.Request.Form.Files
                    |> Seq.fold (fun acc file -> sprintf "%s\n%s" acc file.FileName) ""
                    |> text) ctx
        }

let largeFileUploadHandler =
    fun (ctx : HttpContext) ->
        async {
            let formFeature = ctx.Features.Get<IFormFeature>()
            let! form = formFeature.ReadFormAsync CancellationToken.None |> Async.AwaitTask
            return!
                (form.Files
                |> Seq.fold (fun acc file -> sprintf "%s\n%s" acc file.FileName) ""
                |> text) ctx
        }

I used the built in features form ASP.NET Core. For small file uploads you can directly access the files by calling ctx.Request.Form.Files, which returns an IFormCollection where each item is of type IFormFile. This matches exactly what you would get by injecting an IFormFile object into an MVC action as shown in the documented example for small file uploads.

For large file uploads there is an existing IFormFeature inside the Microsoft.AspNetCore.Http.Features namespace that you can use to stream large files. The original implementation can be found here and as you can see, it does exactly what has been described in the documentation for large file uploads.

It uses the MultipartReader to read each section and a KeyValueAccumulator to store the streamed data.

What do you think of my two examples? Is there something you would like to add or extend from a Giraffe point of view?

from giraffe.

dustinmoris avatar dustinmoris commented on July 4, 2024

Hi, just to let you know I will look at your example later this evening and pick this one up next!

from giraffe.

nojaf avatar nojaf commented on July 4, 2024

Thanks!

from giraffe.

nojaf avatar nojaf commented on July 4, 2024

Hi, thanks for these examples. They are great! I had no idea that it could be this easy to process files.
I was able to reduce my own code from 90 lines to 15.

I don't think Giraffe itself needs any extension to work with files.

from giraffe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.