【问题标题】:Create Files in Server from Response.OutputStream从 Response.OutputStream 在服务器中创建文件
【发布时间】:2014-09-15 03:23:57
【问题描述】:

我有一个 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();
}

【问题讨论】:

    标签: c# asp.net stream


    【解决方案1】:

    你没有说你得到了什么错误,但从路径我猜它是访问被拒绝错误。

    如果是这种情况,那么基本上 Web 服务器运行的 Windows 用户没有您所有机器的许可,但仅适用于某些文件夹。 您可以使用 Server.MapPath 将文件保存在站点目录树中, 这是一个例子Save file to web server

    【讨论】:

    • 我的错误是用西班牙语抛出的,翻译是怎么回事:无法读取序列。问题是因为 Response.OutputStream 不可搜索
    • 代码中指定的路径为新建文件的路径和名称
    • 在这种情况下,您是否尝试使用 MemoryStream 包装 OutputStream?
    猜你喜欢
    • 2020-04-20
    • 2016-09-26
    • 2014-11-07
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多