public void ProcessRequest(HttpContext context)
        {
            using (MainDataContext db = new MainDataContext())
            {
                CRM_Communication comm = db.CRM_Communications.Single(s => s.ID.ToString() == context.Request.QueryString["id"]);
                context.Response.Clear();
                context.Response.ClearHeaders();
                context.Response.ClearContent();
                context.Response.AppendHeader("content-length", comm.FileStore.Length.ToString());
                context.Response.ContentType = "text/csv";

                string filename = CRM.Code.Utils.Url.Url.CreateFriendlyUrl(comm.Name) + "-communication-log-export-" + DateTime.UtcNow.ToString("dd-MM-yyyy") + ".csv";
                string format   = "attachment; filename=\"{0}\"";

                context.Response.AppendHeader("content-disposition", String.Format(format, filename));
                context.Response.BinaryWrite(comm.FileStore.ToArray());
                context.ApplicationInstance.CompleteRequest();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                byte[] fileArray = new byte[fuExcel.FileUpload.FileContent.Length + 1];

                byte[] buffer = new byte[16 * 1024];
                using (MemoryStream ms = new MemoryStream())
                {
                    int read;
                    while ((read = fuExcel.FileUpload.FileContent.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        ms.Write(buffer, 0, read);
                    }
                    fileArray = ms.ToArray();
                }

                CRM_Communication communication = new CRM_Communication();
                communication.AdminID          = ((AdminPage)Page).AdminUser.ID;
                communication.Name             = txtName.Text;
                communication.IncomingFileType = fuExcel.Extension;
                communication.TargetReference  = Request.QueryString["target"];
                communication.FileStore        = fileArray;
                communication.DataStore        = new byte[0];
                communication.Timestamp        = UKTime.Now;
                communication.MailType         = Convert.ToByte(ddlType.SelectedValue);
                db.CRM_Communications.InsertOnSubmit(communication);
                db.SubmitChanges();

                int lineNumber = 0;

                fuExcel.FileUpload.FileContent.Seek(0, SeekOrigin.Begin);
                StreamReader reader = new StreamReader(fuExcel.FileUpload.FileContent);
                DataTable    dt     = new DataTable();
                dt.TableName = txtName.Text;

                int ObjectReference = -1;


                while (!reader.EndOfStream)
                {
                    lineNumber++;

                    string contents  = ReadNextMultiline(reader);
                    Regex  CSVParser = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");

                    string[] contentsSplit = CSVParser.Split(contents);


                    if (lineNumber == 1)
                    {
                        for (int i = 0; i < contentsSplit.Length; i++)
                        {
                            if (contentsSplit[i] == "Reference")
                            {
                                ObjectReference = i;
                            }

                            dt.Columns.Add(contentsSplit[i], typeof(string));
                        }
                    }
                    else
                    {
                        if (ObjectReference != -1)
                        {
                            CRM_CommunicationLink link = new CRM_CommunicationLink();

                            link.TargetReference = contentsSplit[ObjectReference];
                            communication.CRM_CommunicationLinks.Add(link);
                            db.SubmitChanges();
                        }

                        DataRow row = dt.NewRow();
                        for (int i = 0; i < contentsSplit.Length; i++)
                        {
                            try
                            {
                                row[i] = contentsSplit[i];
                            }
                            catch (IndexOutOfRangeException ex) { }
                        }
                        dt.Rows.Add(row);
                    }
                }

                BinaryFormatter bformatter = new BinaryFormatter();
                MemoryStream    stream     = new MemoryStream();
                bformatter.Serialize(stream, dt);
                communication.DataStore = stream.ToArray();
                stream.Close();
                dt = null;
                db.SubmitChanges();


                mvLog.SetActiveView(viewDone);
                lblLogResult.Text = communication.Name + " - " + communication.CRM_CommunicationLinks.Count() + " records matched up";
            }
        }