Devs.site

Upload a file using an ASP.NET HTTP handler

In ASP.NET, IHttpHandler takes care of basic requests and API services. It receives the requests and returns a HTTP response. These handlers have the .ashx extension. In this brief script we take an image sent through a file input and save it on the server


The form that submits the form should look like this

<form enctype="multipart/form-data" method="POST" action="/uploads/upload.ashx">
    <label for="upload">Upload file</label>
    <input id="upload" type="file" name="upload">
</form>

The request will be sent to /uploads/upload.ashx, a IHttpHandler file that contains this simple code:

    <%@ WebHandler Language="C#" Class="upload" %>

    using System.Web;

        public class upload : IHttpHandler {

        public void ProcessRequest (HttpContext context) {
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile uploads = context.Request.Files["upload"];
                string directory = context.Server.MapPath("..\\") + "\\uploads\\files\\";
                string file = System.IO.Path.GetFileName(uploads.FileName);
                uploads.SaveAs(directory + file);
                context.Response.Write("File " + file + " uploaded successfully!");
            } else
            {
                context.Response.Write("Nothing uploaded");
            }
            context.Response.End();
        }

        public bool IsReusable {
            get {
                return false;
            }
        }
    }

In this handler we check if any file has been uploaded and if true, we get the file name then save it to the server inside the uploads\files\ folder. We must make sure that this folder exists and is writable before running the script.

This can be done before uploading the file with this statement:

    if (!Directory.Exists(directory) Directory.CreateDirectory(directory);

Code

    <%@ WebHandler Language="C#" Class="upload" %>

    using System.Web;

    public class upload : IHttpHandler {

        public void ProcessRequest (HttpContext context) {
            if (context.Request.Files.Count > 0)
            {
                HttpPostedFile uploads = context.Request.Files["upload"];
                string directory = context.Server.MapPath("..\\") + "\\uploads\\files\\";
                string file = System.IO.Path.GetFileName(uploads.FileName);

                if (!Directory.Exists(directory) Directory.CreateDirectory(directory);

                uploads.SaveAs(directory + file);
                context.Response.Write("File " + file + " uploaded successfully!");
            } else
            {
                context.Response.Write("Nothing uploaded");
            }
            context.Response.End();
        }

        public bool IsReusable {
            get {
                return false;
            }
        }
    }

Requirements


Instructions

Create the .ashx file by adding a new file to the project (Visual C# / Generic Handler)

0 comments

Specify your e-mail if you want to receive notifications about new comments and replies