显示PDF ASP.Net MVC [英] Displaying PDF ASP.Net MVC

查看:233
本文介绍了显示PDF ASP.Net MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的桌面测试上的文件。我想在像这样一个视图来显示它:

I have a file on my desktop for test. I am trying to display it in a view that looks like this:

@{
    ViewBag.Title = "ShowFile";
}

<h2>ShowFile</h2>

在code我使用控制器是:

The code I am using for the controller is:

   [HttpGet]
        public ActionResult ShowFile(string path)
        {
            path = @"C:\Documents and Settings\nickla\Desktop\nlamarca_06_15.pdf";

            return File(path, "application/pdf", "nlamarca_06_15.pdf");
        }

当我运行这个code中的视图显示未定义什么可能是错在这里的任何想法?

When I run this code the view displays "undefined" any ideas on what could be wrong here?

推荐答案

您似乎没有指定在你的路径文件名:

You don't seem to have specified the filename in your path:

public ActionResult ShowFile(string filename)
{
    var path = @"C:\Documents and Settings\nickla\Desktop";
    var file = Path.Combine(path, filename);
    file = Path.GetFullPath(file);
    if (!file.StartsWith(path))
    {
        // someone tried to be smart and sent 
        // ?filename=..\..\creditcard.pdf as parameter
        throw new HttpException(403, "Forbidden");
    }
    return File(file, "application/pdf");
}

这篇关于显示PDF ASP.Net MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆