How to Upload File and Images Inside Deno Server
Tags: deno, typescript, codenewbie
Published: 2020-06-24 16:33:20

<p>

Welcome again to my blog, today we gonna learn how to upload files or images inside Deno, as uploading files or images inside Node can be handled with Multer but in Deno handling file upload is still a mystery so let unwrap this…
</p>
![Getting Started](https://dev-to-uploads.s3.amazonaws.com/i/e75v3prrnxg2olr32bzw.png)
<p>
For uploading our we gonna need Oak Middleware Upload module from Deno‘s website. We also add other modules inside our project which include Oak and View Engine to server our webpage and manage our application. So let’s set our <a href="https://medium.com/recoding/rendering-html-css-in-deno-using-view-engine-e07469613598">View Engine boilerplate codes<a>.

</p>

<pre>

// Importing Module
import {Application, Router} from 'https://deno.land/x/oak/mod.ts';
import {viewEngine,
engineFactory,
adapterFactory} from 'https://deno.land/x/view_engine/mod.ts';
import {upload} from 'https://deno.land/x/upload_middleware_for_oak_framework/mod.ts';
// Setting up our view Engine
const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();
// Initiate our Application and Router
const app = new Application();
const router = new Router();
app.use(viewEngine(oakAdapter, ejsEngine));
// Setting our router to handle request
router
 .get('/', (ctx) => {
  ctx.render('index.ejs')
 });
// Passing Router as middleware
app.use(router.routes());
app.use(router.allowedMethods());
// Server our app
console.log('App is listening on PORT 8000');
await app.listen({port: 8000});

</pre>

<p>

Now inside our <b>“index.ejs”</b> let’s a basic form which has a method of “POST” to the path “/upload” and enctype as “multipart/form-data”. Inside the form, tag adds an input tag which is type of “file” and name as “fileName” you can set anything you want. Our index.ejs file gonna look something like.

</p>

<pre>

&lt;body&gt;
&lt;form method="POST" enctype="multipart/form-data" action="/validated"&gt;
 &lt;input type="file" name="fileName" multiple&gt; &lt;br&gt;
 &lt;input type="submit" value="submit"&gt;
&lt;/form&gt;
&lt;/body&gt;

</pre>

![Form Style](https://dev-to-uploads.s3.amazonaws.com/i/t6cjm87ywl6hznwzq9n9.png)

<p>
Now inside our app.ts file let’s create a method to handle POST request as
</p>

<pre>
.post('/upload', upload('uploads'), async(context: any, next: any)=>{
 const file = context.uploadedFiles;
 console.log(file);
 context.response.redirect('/');
})
</pre>

<p>
Now save your file and open the browser and check if your application is working or not. When you submit the form inside the root file where your application is stored a file upload will be created and you can see that file you have uploaded finally submit.

Our app.ts file finally looks something like this in case you miss something.
</p>

<pre>

// Importing Module
import {Application, Router} from 'https://deno.land/x/oak/mod.ts';
import {viewEngine,
engineFactory,
adapterFactory} from 'https://deno.land/x/view_engine/mod.ts';
import {upload} from 'https://deno.land/x/upload_middleware_for_oak_framework/mod.ts';
// Setting up our view Engine
const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();
// Initiate our Application and Router
const app = new Application();
const router = new Router();
app.use(viewEngine(oakAdapter, ejsEngine));
// Setting our router to handle request
router
.get('/', (ctx) => {
ctx.render('index.ejs')
})
.post('/upload', upload('uploads'), async(context: any, next: any)=>{
const file = context.uploadedFiles;
console.log(file);
context.response.redirect('/');
});
// Passing Router as middleware
app.use(router.routes());
app.use(router.allowedMethods());
// Server our app
console.log('App is listening on PORT 8000');
await app.listen({port: 8000});

</pre>

<p>

So finally we have implemented our uploading file inside the deno server so 😉.

Check our video if you want to prefer to follow our video…
</p>

{% youtube bQgC9LQx97c %}

![Good Bye](https://dev-to-uploads.s3.amazonaws.com/i/tuwd55eezmnevn9tb5jo.gif)