【问题标题】:Solution ASP.net call method from url to view pdf解决方法ASP.net从url调用方法查看pdf
【发布时间】:2022-01-02 12:08:30
【问题描述】:

我正在尝试调用一种方法来查看 PDF 我正在尝试从 URL 调用该方法,例如。 /Home.aspx/ViewPDF

url中的ViewPDF就是Home.aspx文件中的方法。

我是 ASP 新手,这让我很困惑,如果您能提供任何帮助,谢谢

编辑 1: 我在它的 click 方法中有一个带有这个的 asp 按钮,它可以工作:

    protected void test12_Click(object sender, EventArgs e)
    {            
        string FilePath = Server.MapPath("/Tasks/Content/54874/dummy.pdf");
        WebClient User = new WebClient();
        Byte[] FileBuffer = User.DownloadData(FilePath);
        if (FileBuffer != null)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", FileBuffer.Length.ToString());
            Response.BinaryWrite(FileBuffer);

        }
    }

访问/Home.aspx/ViewPDF,我怎么能做到同样的事情呢? /ViewPDF 是 Home.aspx 后面代码中的一个方法 我相信该方法需要是静态的才能从 url 中查看它是否有任何替代方法可以将文件路径定义为 Server.MapPath 不是静态的。

我在Home.aspx中做了一个测试方法:

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public static string test(object sender, EventArgs e)
    {
        return "test";
    }

但是当我访问 /Home.aspx/test 时,它只是加载了一个没有图像并且稍微偏离 css 的页面版本

感谢您的帮助

编辑 2 / 解决方案: 在谷歌上浏览了一下之后,您可以轻松地将 MVC 实现到 Web 表单中 https://www.davepaquette.com/archive/2013/12/30/so-you-inherited-an-asp-net-web-forms-application.aspx

由此我制作了一个控制器

    public FileResult GetTaskAttatchment()
    {
        string ReportURL = Server.MapPath("/Tasks/Content/54874/dummy.pdf");
        byte[] FileBytes = System.IO.File.ReadAllBytes(ReportURL);
        return File(FileBytes, "application/pdf");        
    }

现在完美运行,再次感谢所有帮助

【问题讨论】:

  • 如果您是 Asp.net 网络表单的新手,请不要使用它。现在是遗留系统。您应该使用 .net core MVC 而不是 .net 框架。 More about this
  • 这里是 Asp.net 中的示例代码aspsnippets.com/questions/112991/…
  • 感谢您的回复,我目前有 MVC 控制器,可以从 URL 向我显示 PDF,但是我的项目的其余部分是 Web 表单,所以我不得不创建一个新项目,是无论如何将控制器包含在我的原始网络表单中?
  • 是的,我在一个项目中做过,但那是很久以前的事了。所以非常有可能使用。
  • 谢谢,我现在去看看是否有任何显示

标签: c# asp.net


【解决方案1】:

//检查附件的数据类型应该是 varbinary...

protected void btnfinalupload_Click(object sender, EventArgs e) {

        string filename = "";
        string filenameret = "";
        using (DataSet ds = GetFormForDownload(id,finid))
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                filenameret = ds.Tables[0].Rows[0]["attachment"].ToString().Trim();
                filename = ds.Tables[0].Rows[0]["filename"].ToString().Trim();

                byte[] bytes;
                string fileName, contentType;
                bytes = (byte[])ds.Tables[0].Rows[0]["attachment"];
                contentType = "application/pdf";
                fileName = filename;
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "";
                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.ContentType = contentType;
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
                Response.BinaryWrite(bytes);
                Response.Flush();
                Response.End();
            }
        }
    

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 2011-04-04
    • 2020-02-23
    • 2019-03-05
    • 2011-06-27
    • 1970-01-01
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多