Пример #1
0
        public Object DownloadFile(int id)
        {
            var rootPath = HttpContext.Current.Server.MapPath("~/UploadedFiles");

            var dao  = new FileDAO();
            var file = dao.GetById(id);

            if (file != null)
            {
                HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);
                var fileFullPath         = Path.Combine(rootPath, file.Name + file.FileExt);

                byte[] fileStream         = System.IO.File.ReadAllBytes(fileFullPath);
                System.IO.MemoryStream ms = new MemoryStream(fileStream);

                resp.Content = new ByteArrayContent(fileStream);
                resp.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");

                var type = MimeTypeMap.GetMimeType(file.FileExt);
                resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(type);
                resp.Content.Headers.ContentDisposition.FileName = file.Name.Substring(file.Name.IndexOf('-') + 1) + file.FileExt;
                return(resp);
            }
            else
            {
                HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.NotFound);
                return(resp);
            }
        }
Пример #2
0
        public void Insert__WithCorrectData__ReturnsReturnsIdOfInsertedRecord()
        {
            File file = new File();

            file.Name          = "Text File";
            file.ParenFolderId = -1;
            file.IsActive      = true;
            file.FileExt       = ".txt";
            file.FileSizeInKB  = 100;
            file.CreatedBy     = 1;
            file.UploadedOn    = DateTime.Now.Truncate(TimeSpan.FromSeconds(1));

            file.Id = dao.Insert(file);

            var dbFile = dao.GetById(file.Id);

            Assert.AreEqual(dbFile.Id, file.Id);
            Assert.AreEqual(dbFile.Name, file.Name);
            Assert.AreEqual(dbFile.ParenFolderId, file.ParenFolderId);
            Assert.AreEqual(dbFile.IsActive, file.IsActive);
            Assert.AreEqual(dbFile.FileSizeInKB, file.FileSizeInKB);
            Assert.AreEqual(dbFile.FileExt, file.FileExt);
            Assert.AreEqual(dbFile.CreatedBy, file.CreatedBy);
            Assert.AreEqual(dbFile.UploadedOn, file.UploadedOn);
        }
Пример #3
0
        public void Delete(int id)
        {
            FileDAO dao = new FileDAO();

            var file = dao.GetById(id);

            file.IsActive = false;
            file.Update();
        }