Пример #1
0
        public IHttpActionResult Generate([FromUri]string format)
        {
            IGenerate generator = null;
            byte[] content;

            switch (format)
            {
                case "pdf":
                    {
                        generator = new GeneratePDF();
                        break;
                    }
                default:
                    return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Invalid format"));

            }

            List<food> foods = _db.food.ToList();
            content = generator.Generate(foods);

            string pdfName = "Foods";

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/" + format;
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + pdfName + "." + format);
            HttpContext.Current.Response.ContentType = "application/" + format;
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            HttpContext.Current.Response.BinaryWrite(content);
            HttpContext.Current.Response.End();
            HttpContext.Current.Response.Close();

            return Ok();
        }
Пример #2
0
        public HttpResponseMessage GenereteFile(GeneratePDF GenFile)
        {
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest);

            byte[] buffer        = (byte[])GenFile.Generate();
            var    contentLength = buffer.Length;
            var    statuscode    = HttpStatusCode.OK;

            response         = Request.CreateResponse(statuscode);
            response.Content = new StreamContent(new MemoryStream(buffer));
            response.Content.Headers.ContentType   = new MediaTypeHeaderValue("application/pdf");
            response.Content.Headers.ContentLength = contentLength;
            ContentDispositionHeaderValue contentDisposition = null;
            string RandomeFile = Guid.NewGuid().ToString();

            if (ContentDispositionHeaderValue.TryParse("inline; filename=" + RandomeFile + ".pdf", out contentDisposition))
            {
                response.Content.Headers.ContentDisposition = contentDisposition;
            }
            return(response);
        }