Search code examples
c#asp.netfileasp.net-web-apiopenxml

download File with openXML


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 retrieves me a memoryStream, I basically 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, basically I want to create a document with a table, and download it when the user performs a request to that access point.


Solution

  • Create the word document with the MemoryStream you define

    private MemoryStream createDocument(Table customTable)
    {
        var stream = new MemoryStream();
    
        // Create a Wordprocessing document. 
        using (WordprocessingDocument package = WordprocessingDocument.Create(stream, 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);
        return stream;
    }
    

    The previous function solves the requirement of creating a Word file and buffering the data. Now we need to take the buffered data and send it on its way to the client as a text file. The following snippet shows how this is done.

    private void PageLoad(object sender, System.EventArgs e)
    {
        System.IO.MemoryStream mstream = createDocument();
        byte[] byteArray = mstream.ToArray();
        mstream.Flush();
        mstream.Close();
        Response.Clear();
        // Add a HTTP header to the output stream that specifies the default filename
        // for the browser's download dialog
        Response.AddHeader("Content", "attachment; filename=foo.docx");
        // Add a HTTP header to the output stream that contains the 
        // content length(File Size). This lets the browser know how much data is being transfered
        Response.AddHeader("Content", byteArray.Length.ToString());
        // Set the HTTP MIME type of the output stream
        Response.ContentType = "application/octet-stream";
        // Write the data out to the client.
        Response.BinaryWrite(byteArray);
    }
    

    Hope this helps