locked
Download file openXml RRS feed

  • Question

  • User287566428 posted

    Hey guys,

    I am doing a request to this controller:

            [ResponseType(typeof(Book))]
            public async Task<IHttpActionResult> PostBook(Book book)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
    
                string path = "C:\\Users\\af-costa\\Desktop\\TEST.docx";
                var stream = new MemoryStream();
    
                db.Books.Add(book);
                await db.SaveChangesAsync();
    
                // New code:
                // Load author name
                db.Entry(book).Reference(x => x.author).Load();
    
                var dto = new BookDTO()
                {
                    Id = book.Id,
                    Title = book.Title,
                    AuthorName = book.author.name
                };
    
                Table customTable = createTable(book.author.name, book.Title);
                stream = createDocument(customTable);
    
                return CreatedAtRoute("DefaultApi", new { id = book.Id }, dto);
            }


    the createDocument retrives me a memoryStream, i basicly need to download the file on the stream that i get from the openXml, in this method:

     private void createDocument(Table customTable)
            {
                var stream = new MemoryStream();
                // Create a Wordprocessing document. 
                using (WordprocessingDocument package = WordprocessingDocument.Create("Test1", WordprocessingDocumentType.Document))
                {
                    // Add a new main document part. 
                    package.AddMainDocumentPart();
    
                    // Create the Document DOM. 
                    package.MainDocumentPart.Document =
                      new Document(
                        new Body(
                          new Paragraph(
                            new Run(
                              new Table(customTable)))));
    
                    // Save changes to the main document part. 
                    package.MainDocumentPart.Document.Save();
                }
                stream.Seek(0, SeekOrigin.Begin);
     
            }

    i don't know very well how can i download that file, basicly i want to create a document with a table, and download it when the user performs a request to
    that access point, any help?




    Thursday, July 13, 2017 1:49 PM

All replies

  • User1168443798 posted

    Hi FilipeCosrta,

    >> i don't know very well how can i download that file, basicly i want to create a document with a table, and download it when the user performs a request to that access point, any help?

    Have you created the document correctly?

    If you have, I would suggest you try below code to download file.

            [HttpGet]
            [Route("api/values/DownLoad")]
            public HttpResponseMessage DownLoad()
            {
                string filePath = @"full file path";
                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentType =
                    new MediaTypeHeaderValue("application/octet-stream");
                result.Content.Headers.ContentDisposition= new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "file name.docx"
                };
    
                return result;
            }

    Best Regards,

    Edward

    Friday, July 14, 2017 6:25 AM