0

I have a aspx page that creates a PDF file to Download to the client´s browser and it works fine. But I need to create that file into the server too and that´s the problem.

How i can to transform Response.OutputStream to a File into the server?

here is my code:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();

        DestinationPDF.DestinationPDF.DestinationPDFParameters dPdfParams = jss.Deserialize<DestinationPDF.DestinationPDF.DestinationPDFParameters>(Request["destinationPdfValue"]);
        dPdfParams.DocAttributes.MarginBottom = 10;
        dPdfParams.DocAttributes.MarginLeft = 10;
        dPdfParams.DocAttributes.MarginRight = 10;
        dPdfParams.DocAttributes.MarginTop = 10;
        dPdfParams.DocAttributes.FileName = "Acreditacion_" + Request["id"];

        DestinationPDF.DestinationPDF dPdf = new DestinationPDF.DestinationPDF(dPdfParams.Widgets,dPdfParams.DocAttributes);

        Response.AppendHeader("Content-disposition", string.Format("attachment;filename={0}", dPdf.GetFileName()));
        Response.ContentType = "application/pdf";


        dPdf.Save(Response.OutputStream);
        Stream stream = Response.OutputStream;
        var fileStream = new FileStream(@"C:\PROYECTOS 2013\CANCILLERIA\PortalTramites\PortalTramites\Documentos\pdf__Acreditacion" + Request["id"] + ".pdf", FileMode.Create, FileAccess.Write);
        stream.CopyTo(fileStream, 256);
    }
    catch (Exception ex) { Response.End(); }
    Response.End();
}
Aloti
  • 67
  • 1
  • 9

1 Answers1

0

You did not said what error you are getting, but from the path I am guessing that it is access denied error.

If that is the case then basically web server running with a windows user that dont have permitions for all your machine but only for certain folders. You can save the file inside your site directory tree by using Server.MapPath, here is an example Save file to web server

Community
  • 1
  • 1
Nati Dobkin
  • 81
  • 1
  • 4
  • My Error is thrown in spanish, translate is something how: The sequence can not be readed it. The problem is beacause the Response.OutputStream is not Seekable – Aloti Sep 14 '14 at 19:51
  • the path specified in the code is the path and name of the new file – Aloti Sep 14 '14 at 19:58
  • In that case have you tried to wrap the OutputStream with MemoryStream? – Nati Dobkin Sep 14 '14 at 21:00