从Response.OutputStream在服务器中创建文件

本文关键字:创建 文件 服务器 Response OutputStream | 更新日期: 2023-09-27 18:21:46

我有一个aspx页面,它可以创建一个PDF文件下载到客户端浏览器,并且运行良好。但我也需要将该文件创建到服务器中,这就是问题所在。

如何将Response.OutputStream转换为服务器中的文件?

这是我的代码:

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();
}

从Response.OutputStream在服务器中创建文件

您没有说明您遇到了什么错误,但从路径上看,我猜这是拒绝访问错误。

如果是这样的话,那么基本上web服务器运行的是一个windows用户,该用户没有对所有机器的权限,但只对某些文件夹有权限。您可以使用Server.MapPath将文件保存在站点目录树中,下面是一个将文件保存到web服务器

的示例