Ejemplo n.º 1
0
    protected void LessonAttachmentRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        ApplicationDbContext dbContext = new ApplicationDbContext();

        if (e.CommandName == "Download")
        {
            int attachmentId = int.Parse(e.CommandArgument.ToString());

            LessonAttachment lessonAttachment = dbContext.LessonAttachments.Where(la => la.Id == attachmentId).FirstOrDefault();

            string   fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + "." + lessonAttachment.FileType;
            FileInfo fileInfo = new FileInfo(fileName);

            using (var stream = fileInfo.OpenWrite())
            {
                stream.Write(lessonAttachment.Data, 0, lessonAttachment.Data.Count());
            }

            Response.ContentType = lessonAttachment.FileType;
            Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.{1}", lessonAttachment.Title, lessonAttachment.FileType));
            Response.TransmitFile(fileInfo.FullName);
            Response.End();
        }
        else if (e.CommandName == "Upload")
        {
        }
    }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddAttachment(IFormFile file, int lessonId)
        {
            LessonAttachment attachment =
                await _attachmentService.AddAsync(file, AttachmentType.Lesson) as LessonAttachment;

            attachment.LessonId = lessonId;

            await _db.SaveChangesAsync();

            return(Ok(_mapper.Map <AttachmentDto>(attachment)));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> DeleteAttachment(int id)
        {
            LessonAttachment entity = await _db.LessonAttachments.SingleOrDefaultAsync(x => x.Id == id);

            if (entity == null)
            {
                return(NotFound());
            }
            _db.LessonAttachments.Remove(entity);
            await _db.SaveChangesAsync();

            return(Ok());
        }
Ejemplo n.º 4
0
    protected void LessonAttachmentButton_Click(object sender, EventArgs e)
    {
        if (LessonAttachmentUpload.HasFile)
        {
            try
            {
                ApplicationDbContext dbContext = new ApplicationDbContext();

                string fileName = Path.GetFileName(LessonAttachmentUpload.FileName);
                string fileType = Path.GetExtension(fileName).Replace(".", "");

                LessonAttachment lessonAttachment = new LessonAttachment();

                lessonAttachment.Title    = LessonAttachmentUpload.FileName;
                lessonAttachment.FileType = fileType;
                lessonAttachment.Data     = LessonAttachmentUpload.FileBytes;

                Lesson CurrentLesson = (Lesson)Session["CurrentLesson"];

                lessonAttachment.Lesson = dbContext.Lessons.Where(l => l.Id == CurrentLesson.Id).FirstOrDefault();

                dbContext.LessonAttachments.Add(lessonAttachment);

                dbContext.SaveChanges();

                LessonAttachmentRepeater.DataBind();

                HomePanel.Visible   = false;
                LessonPanel.Visible = true;

                ActivePanelLabel.Text = "Lessons";
            }
            catch
            {
            }
        }
    }