0

I have a drag and drop File Control and also making use of the FileHandler class. Currently it saves the file to a folder in my pc however i would like to save the file to the database, how can i achieve that?

Here is the code for my FileHandler class:

public class FileHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection files = context.Request.Files;
            foreach (string key in files)
            {
                HttpPostedFile file = files[key];
                string fileName = file.FileName;
                fileName = context.Server.MapPath("~/ApplicantUploads/" + fileName);
                file.SaveAs(fileName);
            }
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write("File(s) uploaded successfully!");
    }

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

And since im using a 3 tier achictecture have also tried it like this:

public class FileHandler : IHttpHandler 
{
    BusinessLogicLayer bll = new BusinessLogicLayer();
    public void ProcessRequest(HttpContext context)
    {
        string StudNum = HttpContext.Current.Session["StudNum"].ToString();
        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection files = context.Request.Files;
            foreach (string key in files)
            {
                HttpPostedFile file = files[key];
                string contentType = file.ContentType;
                string fileName = file.FileName;
                using (Stream fs = file.InputStream)
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        byte[] bytes = br.ReadBytes((Int32)fs.Length);
                        ApplicantDocuments apd = new ApplicantDocuments(fileName, StudNum, contentType, bytes);

                        int x = bll.UploadDocuments(apd);
                    }
                }

                //fileName = context.Server.MapPath("~/ApplicantUploads/" + fileName);
                //file.SaveAs(fileName);
            }
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write("File(s) uploaded successfully!");
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
  • Instead of file.SaveAs(fileName); write to the database. – jdweng Dec 14 '20 at 19:11
  • You need to save filename and file bytes separately in database. – Pirate Dec 14 '20 at 19:12
  • @Convert.ToInt32 I would suggest storing files on a file server not in a database and then store some `metadata` about the file inside your database. But if you really want to store them in a DB, then you can read about it more here: (https://stackoverflow.com/questions/2579373/saving-any-file-to-in-the-database-just-convert-it-to-a-byte-array) – Ryan Wilson Dec 14 '20 at 19:16
  • Thank you guys. had to follow @RyanWilson suggestion – Convert.ToInt32 Dec 15 '20 at 18:31

0 Answers0