Calling Document.Save(..) method in Asp.net MVC 4 controller action method +

Hi,

I want to download a file from server and then save it using Aspose.Document.Save(…) method inside the Asp.net MVC 4 controller action method.

But, I am facing difficulty in getting the “HttpResponse Response” value for 1st parameter in Save() method inside this controller method…None of the methods that I used worked here.

I tried using HttpContext.ApplicationInstance.Response and other ways. I also read in one of the post @ https://forum.aspose.com/t/52441 that related to HttpResponse class, HttpResponseWrapper class & HttpResponseWrapper class.But couldn’t get the working soluiton.

Could you please provide me a sample code to get the HttpResponse that can be passed to Document.Save( ) method.

My code is as shown below:

public virtual ActionResult ViewDocument(String DocRevisionPath)
{
    try
    {
        if (!String.IsNullOrEmpty(DocRevisionPath))
        {
            String FileName = Path.GetFileName(DocRevisionPath); //fileName will have the .doc extn.
            Document document = new Document();
            DCPlanFileStorage fileStorage = new DCPlanFileStorage();
            byte[] byteArray = fileStorage.DownloadFileStream(DocRevisionPath);
            using (Stream stream = new MemoryStream(byteArray))
            {
                document = new Document(stream);
            }
            document.Save(Response, FileName, ContentDisposition.Attachment, null); //Having problem here in getting the Response parameter.Need help**
        }
        return Json("success", JsonRequestBehavior.AllowGet);
    }
    catch
    {
        return Json("Failure", JsonRequestBehavior.AllowGet);
    }
}

Hi,

I am able to get the HttpResponse parameter by using “System.Web.HttpContext.Current.Response”.

However, The following .Save(…) statement is not giving the dialog with opiton to Open/Save a word document when executed.

document.Save(System.Web.HttpContext.Current.Response, FileName, ContentDisposition.Attachment, null);

The FileName parameter will be a file name with .doc extn. I tried even passing the ContentDisposition as “Inline” also.

I am using .NET framework 4.0 and MVC 4.

Please let me know what should I do to get the dialog with Open/Save option in the UI after document.Save() method is executed similar to the Aspose.Words demo programs.

Hi Santosh,

Thanks for your inquiry. It is nice to hear from you that you have solved the HttpResponse issue. Regarding your second question (save dialog option), it is related to browser settings. The Document.Save Method (HttpResponse, String, ContentDisposition, SaveOptions) method only sends the document to the client browser.

Please see the attached image for download file’s settings of Google Chrome. Hope this answers your query. Please let us know if you have any more queries.

Hi,

Earlier, I was using the net2.0 Aspose.Words.dll in my asp.net MVC 4.0 application. Due to this the Document.Save (HttpResponse, String, ContentDisposition, SaveOptions) method was accessible in my MVC controller action method. But nothing was happening after execution.

Actually, I wanted to use the latest version of Asponse.Words.dll from bin/net3.5_ClientProfile. When I used this dll, I am not getting the Save overloadd method that takes the HttpResponse as its parameter.So, I have to save the document to local file.

public virtual ActionResult ViewDocument(String DocRevisionPath)
{
    ........

    Stream dataStream = fileStorage.DownloadFileStream(DocRevisionPath), ref exception);
    Document AarowDoc = new Document(dataStream);
    AarowDoc.Save("C:\\AsposeDoc.doc", SaveFormat.Doc);
    //return File(dataStream, "application/msword", FileName);
    ....................
}

But, I need to send this saved document or Stream to HttpResponsebase Response as an attachment from this controller method, so that user gets an option to either Save OR Open this document.

Basically I want to get/acheive the similar functionality as 'net2.0 Document.Save (HttpResponse, String, ContentDisposition, SaveOptions)’ in my asp.net MVC 4 application. Please suggest.

My javasript code in .cshtml is as shown below:

Hi,

Earlier, I was using the net2.0 Aspose.Words.dll in my asp.net MVC 4.0 application. Due to this the Document.Save (HttpResponse, String, ContentDisposition, SaveOptions) method was accessible in my MVC controller action method. But nothing was happening after execution.

Actually, I wanted to use the latest version of Asponse.Words.dll from bin/net3.5_ClientProfile. When I used this dll, I am not getting the Save overloadd method that takes the HttpResponse as its parameter.So, I have to save the document to local file.

public virtual ActionResult ViewDocument(String DocRevisionPath)
{
    ........

    Stream dataStream = fileStorage.DownloadFileStream(DocRevisionPath), ref exception);
    Document AarowDoc = new Document(dataStream);
    AarowDoc.Save("C:\\AsposeDoc.doc", SaveFormat.Doc);
    //return File(dataStream, "application/msword", FileName);
    ....................
}

But, I need to send this saved document or Stream to HttpResponsebase Response as an attachment from this controller method, so that user gets an option to either Save OR Open this document.

Basically I want to get/acheive the similar functionality as 'net2.0 Document.Save (HttpResponse, String, ContentDisposition, SaveOptions)’ in my asp.net MVC 4 application. Please suggest.

My javasript code in .cshtml is as shown below:

Hi Santosh,

Thanks for your inquiry. Please note that Aspose.Words DLL from net2.0 folder is used for 2.0, 3.0, 3.5 and 4.0 .Net Frameworks. This assembly cannot be used when targeting the .NET Framework 3.5 or 4.0 Client Profile as it depends on System.Web. In such an environment use the library under net3.5_ClientProfile instead.

In your case, please use net2.0 assembly of Aspose.Words. Hopefully it will serve your purpose. Please read more about Document.Save methods from here:

https://reference.aspose.com/words/net/aspose.words/document/save/

Moreover, the net3.5_ClientProfile folder contains assemblies to use with .NET Framework 3.5 or 4.0 Client Profile. Note that this assembly excludes the Save overload accepting an HttpResponse object. This is by design as the client profile excludes System.Web assembly.

Hope this answers your query. Please let us know if you have any more queries.