C# 防止代码提示用户保存文件

C# 防止代码提示用户保存文件,c#,asp.net-mvc,C#,Asp.net Mvc,我在我的报表控制器中使用下面的方法。它成功地返回我分配给数据库中varBinary(MAX)字段的文件内容。我的问题是,当执行此代码时,浏览器会提示用户保存或打开文件。我想阻止这一切发生 如何强制它只将二进制数据返回到调用控制器方法,而不将结果推送到客户端浏览器 private FileContentResult RenderReportFile(LocalReport localReport, List<ReportDataSource> listDS, string sFilen

我在我的报表控制器中使用下面的方法。它成功地返回我分配给数据库中varBinary(MAX)字段的文件内容。我的问题是,当执行此代码时,浏览器会提示用户保存或打开文件。我想阻止这一切发生

如何强制它只将二进制数据返回到调用控制器方法,而不将结果推送到客户端浏览器

private FileContentResult RenderReportFile(LocalReport localReport, List<ReportDataSource> listDS, string sFilename, string sReportType, bool bLandscape)
{
    string sHeight = "11";
    string sWidth = "8.5";

    if (bLandscape)
    { sWidth = sHeight; sHeight = "8.5"; }

    foreach (ReportDataSource ds in listDS)
    {
        localReport.DataSources.Add(ds);
    }

    HttpContextBase imageDirectoryPath = HttpContext;

    string reportType = sReportType;
    string mimeType;
    string encoding;
    string fileNameExtension;

    //The DeviceInfo settings should be changed based on the reportType
    string deviceInfo =
        "<DeviceInfo>" +
        "  <OutputFormat>" + sReportType + "</OutputFormat>" +
        "  <PageWidth>" + sWidth + "in</PageWidth>" +
        "  <PageHeight>" + sHeight + "in</PageHeight>" +
        "  <MarginTop>0.5in</MarginTop>" +
        "  <MarginLeft>0.5in</MarginLeft>" +
        "  <MarginRight>0.5in</MarginRight>" +
        "  <MarginBottom>0.5in</MarginBottom>" +
        "</DeviceInfo>";

    Warning[] warnings;
    string[] streams;
    byte[] renderedBytes;

    //Render
    renderedBytes = localReport.Render(
        reportType,
        deviceInfo,
        out mimeType,
        out encoding,
        out fileNameExtension,
        out streams,
        out warnings);


    //Write to the outputstream
    //Set content-disposition to "attachment" so that user is prompted to take an action
    //on the file (open or save)

    Response.Clear();
    Response.ContentType = mimeType;
    Response.AddHeader("content-disposition", "attachment; filename=" + sFilename + "." + fileNameExtension);
    Response.BinaryWrite(renderedBytes);
    Response.End();
    return File(renderedBytes, "application/pdf", sFilename + "." + fileNameExtension);
}
private FileContentResult RenderReportFile(LocalReport LocalReport,List listDS,string sFilename,string sReportType,bool bLandscape)
{
字符串sHeight=“11”;
字符串sWidth=“8.5”;
如果(乏味的风景)
{sWidth=sHeight;sHeight=“8.5”;}
foreach(listDS中的ReportDataSource ds)
{
localReport.DataSources.Add(ds);
}
HttpContextBase imageDirectoryPath=HttpContext;
string reportType=sReportType;
字符串模拟类型;
字符串编码;
字符串文件名扩展名;
//应根据报告类型更改DeviceInfo设置
字符串设备信息=
"" +
“”+sReportType+“”+
“+sWidth+”在”+
“+Sheigh+”在”+
“0.5英寸”+
“0.5英寸”+
“0.5英寸”+
“0.5英寸”+
"";
警告[]警告;
字符串[]流;
字节[]渲染字节;
//渲染
renderedBytes=localReport.Render(
报告类型,
deviceInfo,
输出mimeType,
输出编码,
输出文件名扩展名,
流出的溪流,
发出警告);
//写入输出流
//将内容处置设置为“附件”,以便提示用户采取操作
//在文件上(打开或保存)
Response.Clear();
Response.ContentType=mimeType;
Response.AddHeader(“内容处置”、“附件;文件名=“+sFilename+””+文件名扩展名);
BinaryWrite(renderBytes);
Response.End();
返回文件(renderdbytes,“application/pdf”,sFilename+“+filename扩展名);
}

此代码负责将文档发送到客户端:

Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=" + sFilename + "." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();

删除它,用户将不会被提示保存或打开。

不要使用内容处理。为什么不将其推送到客户端?你想用它做什么?为什么你要直接操纵
响应