পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Thursday, March 2, 2017

Koa.js - File Uploading

Web applications need to provide the functionality to allow file uploads. Let us see how we can receive files from the clients and store them on our server.
We have already used the koa-body middleware for parsing requests. This middleware is also used for handling file uploads. Let us create a form that allows us to upload files and then save these files using koa. First create a template called file_upload.pug with the following contents:

html
    head
        title File uploads
    body
        form(action="/upload" method="POST" enctype="multipart/form-data")
            div
                input(type="text" name="name" placeholder="Name")
            div
                input(type="file" name="image")
            div
                input(type="submit")
Note that you need to give the same encoding type as above in your form. Now let us handle this data on our server:
var koa = require('koa');
var router = require('koa-router');
var bodyParser = require('koa-body');
var app = koa();

//Set up Pug
var Pug = require('koa-pug');
var pug = new Pug({
  viewPath: './views',
  basedir: './views',
  app: app 
});

//Set up body parsing middleware
app.use(bodyParser({
    formidable:{uploadDir: './uploads'},    //This is where the files would come
    multipart: true,
    urlencoded: true
}));

var _ = router(); //Instantiate the router

_.get('/files', renderForm);
_.post('/upload', handleForm);

function * renderForm(){
    this.render('file_upload');
}
function *handleForm(){
    console.log("Files: ", this.request.body.files);
    console.log("Fields: ", this.request.body.fields);
    this.body = "Received your data!"; //This is where the parsed request is stored
}

app.use(_.routes()); 

app.listen(3000);
When you run this you get a form:
When you submit this, your console will show you an output like:
The files that were uploaded are stored in the path in above output. You can access the files in the request using this.request.body.files and the fields in that request by this.request.body.fields.

No comments:

Post a Comment