How to intercept the output stream of the current actionresult in .NET MVC3?

Hi and thanks for watching!

Background

I am using a Rotativa pdf tool to read a presentation (html) to a PDF file. It works great, but it does not offer a way to save PDF to the file system. Rather, it returns the file to the user browser as a result of the action.

Here is what this code looks like:

public ActionResult PrintQuote(FormCollection fc)
        {
            int revisionId = Int32.Parse(Request.QueryString["RevisionId"]);

            var pdf = new ActionAsPdf(
                 "Quote",
                 new { revisionId = revisionId })
                       {
                           FileName = "Quote--" + revisionId.ToString() + ".pdf",
                           PageSize = Rotativa.Options.Size.Letter
                       };

            return pdf;

        } 

This code calls another actionresult ("Quote"), converts it to PDF, and then returns the PDF as a file upload to the user.

Question

How to intercept a stream of files and save a PDF in my file system. It is true that the PDF code is sent to the user, but my client also wants the PDF file to be saved in the file system at the same time.

Any ideas?

Thank!

Matt

+5
6

, :

HTTP- URL- . , , .

:

    // Returns the results of fetching the requested HTML page.
    public static void SaveHttpResponseAsFile(string RequestUrl, string FilePath)
    {
        try
        {
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(RequestUrl);
            httpRequest.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            httpRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)httpRequest.GetResponse();
            }
            catch (System.Net.WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError)
                    response = (HttpWebResponse)ex.Response;
            }

            using (Stream responseStream = response.GetResponseStream())
            {
                Stream FinalStream = responseStream;
                if (response.ContentEncoding.ToLower().Contains("gzip"))
                    FinalStream = new GZipStream(FinalStream, CompressionMode.Decompress);
                else if (response.ContentEncoding.ToLower().Contains("deflate"))
                    FinalStream = new DeflateStream(FinalStream, CompressionMode.Decompress);

                using (var fileStream = System.IO.File.Create(FilePath))
                {
                    FinalStream.CopyTo(fileStream);
                }

                response.Close();
                FinalStream.Close();
            }
        }
        catch
        { }
    }

:

SaveHttpResponseAsFile("http://localhost:52515/Management/ViewPDFInvoice/" + ID.ToString(), "C:\\temp\\test.pdf");

! , PDF , , .

+3
 return new Rotativa.ActionAsPdf("ConvertIntoPdf") 
 { 
     FileName = "Test.pdf", PageSize = Rotativa.Options.Size.Letter
 };
+1

MVC : http://www.simple-talk.com/content/file.ashx?file=6068

OnResultExecuted() ActionResult.

ActionFilter OnResultExecuted .

Edit: , ActionFilter, ( ) , , .

0

Aaron 'SaveHttpResponseAsFile', , , , ( URL- MVC4).

public static void SaveHttpResponseAsFile(System.Web.HttpRequestBase requestBase, string requestUrl, string saveFilePath)
{
    try
    {
        *snip*
        httpRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
        httpRequest.Headers.Add(HttpRequestHeader.Cookie, requestBase.Headers["Cookie"]);
        *snip*</pre></code>

"" SaveHttpResponseAsFile.

0

Rotativa, .

Using Rotativa;

...

byte[] pdfByteArray = Rotativa.WkhtmltopdfDriver.ConvertHtml( "Rotativa", "-q", stringHtmlResult );

File.WriteAllBytes( outputPath, pdfByteArray );

winforms, PDF Razor Views, -.

0

I managed to get the Eric Brown - Cal solution , but I needed a little tweak to prevent an error that I was getting in a directory that was not found .

(Also, looking at the Rotativa code, it looks like the -q switch is already passed by default, so this may not be necessary, but I did not change it.)

var bytes = Rotativa.WkhtmltopdfDriver.ConvertHtml(Server.MapPath(@"/Rotativa"), "-q", html);
0
source

All Articles