How to save multiple Word documents as one Zip file in ASP .NET MVC DocIO?
Syncfusion® Essential® DocIO is ASP.NET MVC Word feature tour page used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can download multiple Word document files as one ZIP file.
Steps to download multiple Word files as one ZIP file programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.DocIO.AspNet.Mvc5 NuGet package as a reference to your .NET Framework applications from NuGet.org.
- A default controller with name HomeController.cs gets added on the creation of ASP.NET MVC project. Include the following namespaces in the HomeController.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.Compression.Zip;
VB.NET
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLSImports Syncfusion.Compression.Zip
- A default action method named Index will appear in HomeController.cs. Right-click this action method and select Go To View, where you will be directed to its associated view page Index.cshtml.
- Add new button in Index.cshtml as follows.
@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get); { <div> <label> Click the button to view the generated Word document. </label> </div> <div> <input type="submit" value="Create Document" style="width:150px;height:27px" /> </div> } Html.EndForm(); }
- Add a new action method named CreateDocument in HomeController.cs and include the following code snippet to download multiple Word files in zip format in the browser.
C#
//Create zip file ZipArchive zipArchive = new ZipArchive(); //Set compression level for new items zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best; string[] files = Directory.GetFiles(Server.MapPath("/App_Data")); for (int i = 0; i < files.Length; i++) { //Load an existing Word document WordDocument document = new WordDocument(files[i], FormatType.Docx); //Save the document MemoryStream stream = new MemoryStream(); document.Save(stream, FormatType.Docx); //Add the files you want to zip zipArchive.AddItem("Files" + i + ".docx", stream, false, FileAttributes.Normal); //Close the document. document.Close(); } //Zip the filename MemoryStream memoryStream = new MemoryStream(); zipArchive.Save(memoryStream, false); return File(memoryStream.ToArray(), "application/zip", "Attachments.zip");
VB.NET
'Create zip file Dim zipArchive As ZipArchive = New ZipArchive() 'Set compression level for new items zipArchive.DefaultCompressionLevel = Syncfusion.Compression.CompressionLevel.Best 'Get the files Dim files() As String = Directory.GetFiles(Server.MapPath("/App_Data")) Dim i As Integer = 0 Do While (i < files.Length) 'Load an existing Word document Dim document As WordDocument = New WordDocument(files(i), FormatType.Docx) 'Save the document Dim stream As MemoryStream = New MemoryStream document.Save(stream, FormatType.Docx) 'Add the files you want to zip zipArchive.AddItem(String.Concat(i, ".docx"), stream, False, FileAttributes.Normal) 'Close the document document.Close() i = (i + 1) Loop 'Zip the filename Dim memoryStream As MemoryStream = New MemoryStream() zipArchive.Save(memoryStream, False) Return File(memoryStream.ToArray(), "application/zip", "Attachments.zip")
A complete working sample can be downloaded from Sample.zip.
Take a moment to peruse the document where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly the PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion® Word Framework features.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to save multiple Word documents as one Zip file in ASP .NET MVC DocIO.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!