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;
}
}
}