コード例 #1
0
        public HttpResponseMessage DownloadFile([FromUri] FilesDTO fileDTO)
        {
            HttpResponseMessage result   = null;
            FilesDTO            filesDTO = FilesBAL.GetFileByFileIdAndUserId(fileDTO.Id, fileDTO.CreatedBy);

            if (filesDTO == null)
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            else
            {
                var localFilePath = HttpContext.Current.Server.MapPath("~/Uploads/" + filesDTO.Name);
                if (!File.Exists(localFilePath))
                {
                    result = Request.CreateResponse(HttpStatusCode.Gone);
                }
                else
                {
                    result         = Request.CreateResponse(HttpStatusCode.OK);
                    result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
                    result.Content.Headers.ContentDisposition          = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                    result.Content.Headers.ContentDisposition.FileName = filesDTO.Name;
                }
            }
            return(result);
        }
コード例 #2
0
        public HttpResponseMessage GetThumbnail([FromUri] FilesDTO fileDTO)
        {
            HttpResponseMessage response = null;

            fileDTO = FilesBAL.GetFileByFileIdAndUserId(fileDTO.Id, fileDTO.CreatedBy);
            if (fileDTO == null)
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound);
            }
            else
            {
                string path = HttpContext.Current.Server.MapPath("~/Uploads/" + fileDTO.Name);
                if (!File.Exists(path))
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                }
                ShellFile    shellFile  = ShellFile.FromFilePath(path);
                Bitmap       shellThumb = shellFile.Thumbnail.MediumBitmap;
                byte[]       file       = ImageToBytes(shellThumb);
                MemoryStream ms         = new MemoryStream(file);

                response         = Request.CreateResponse(HttpStatusCode.OK);
                response.Content = new ByteArrayContent(file);
                response.Content.Headers.ContentDisposition          = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = fileDTO.Name;
            }
            return(response);
        }