Click or drag to resize
DocumentModelSave Method (Object, String)
Saves the document to a System.Web.HttpResponse or System.Web.HttpResponseBase output stream or a System.Net.Http.HttpResponseMessage and sets appropriate HTTP response headers (Content-Type and Content-Disposition). Use this method in ASP.NET application to send the document to a client browser. File format is resolved from fileName extension. See remarks why httpResponse parameter is of type Object.

Namespace: GemBox.Document
Assembly: GemBox.Document (in GemBox.Document.dll) Version: 25.3.30.1231
Syntax
public void Save(
	Object httpResponse,
	string fileName
)

Parameters

httpResponse
Type: SystemObject
System.Web.HttpResponse object that can be retrieved through the Response property of the System.Web.HttpApplication, System.Web.HttpContext, System.Web.UI.Page or System.Web.UI.UserControl classes or System.Web.HttpResponseBase object or System.Net.Http.HttpResponseMessage object.
fileName
Type: SystemString
File name that will be presented in client browser when downloading a file.
Exceptions
ExceptionCondition
InvalidOperationExceptionIf file extension is not specified or not supported. If file extension is not recognized, use Save(Object, String, SaveOptions) method overload instead.
Remarks
Note Note
Supported only if updating the whole page with a postback and not if doing partial-page updates (when you use UpdatePanel controls to update selected regions of a page).

This method accepts Object rather than HttpResponse, HttpResponseBase or HttpResponseMessage because otherwise GemBox.Document assembly would have a dependency on System.Web or System.Net.Http assemblies and could not be used in applications which target Client Profile .NET Framework.

HttpResponse can be retrieved through the Response property of the HttpApplication, HttpContext, Page or UserControl classes.

For supported file extensions, see remarks for Save(String) method.

Examples

Following examples show how to stream document to a client's browser using various ASP.NET technologies.

ASP.NET Web Forms

Method is defined inside a class that derives from System.Web.UI.Page.

private void ExportDocument()
{
    // Create a new empty document.
    var document = new DocumentModel();

    // Add document content.
    document.Sections.Add(
        new Section(document,
            new Paragraph(document, "Hello world!")));

    HttpResponse response = this.Response;

    // Stream document to browser in DOCX format.
    document.Save(response, "Document.docx");

}

ASP.NET Web Forms

Method is defined inside a class that derives from System.Web.UI.Page.

private void ExportDocument()
{
    // Create a new empty document.
    var document = new DocumentModel();

    // Add document content.
    document.Sections.Add(
        new Section(document,
            new Paragraph(document, "Hello world!")));

    HttpResponseBase responseBase = new HttpResponseWrapper(this.Response);

    // Stream document to browser in DOCX format.
    document.Save(responseBase, "Document.docx");
}

ASP.NET MVC

Method is defined inside a class that derives from System.Web.Mvc.Controller.

public ActionResult ExportDocument()
{
    // Create a new empty document.
    var document = new DocumentModel();

    // Add document content.
    document.Sections.Add(
        new Section(document,
            new Paragraph(document, "Hello world!")));

    byte[] fileContents;

    var options = SaveOptions.DocxDefault;

    // Save document to DOCX format in byte array.
    using (var stream = new MemoryStream())
    {
        document.Save(stream, options);

        fileContents = stream.ToArray();
    }

    // Stream document to browser in DOCX format.
    return File(fileContents, options.ContentType, "Document.docx");
}

ASP.NET Web API

Method is defined inside a class that derives from System.Web.Http.ApiController.

public HttpResponseMessage ExportDocument()
{
    // Create a new empty document.
    var document = new DocumentModel();

    // Add document content.
    document.Sections.Add(
        new Section(document,
            new Paragraph(document, "Hello world!")));

    var responseMessage = new HttpResponseMessage(HttpStatusCode.OK);

    // Save document to response message content in DOCX format.
    document.Save(responseMessage, "Document.docx");

    return responseMessage;
}
See Also