上传和查看文件ASP.NET MVC 5 [英] Upload and View Files ASP.NET MVC 5

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

问题描述

我有这样的code:

    [HttpPost]
    public ActionResult Create(Knowledgebase KB, HttpPostedFileBase file)
    {
        var KBFilePath = "";
        if (ModelState.IsValid)
        {
            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(KB.KnowledgebaseTitle);
                var path = Path.Combine(Server.MapPath("~/Resources/KBArticles"), fileName + ".pdf");
                KBFilePath = path;
                file.SaveAs(path);
            }
            KB.KnowledgebaseLink = KBFilePath;
            db.Knowledgebases.Add(KB);
            db.SaveChanges();
            return RedirectToAction("Index", "Home");
        }

        else
        {
            return View();
        }

的链接,是存储在其中以C开头的DB中的文件路径:/

The link is the filepath which is stored in the DB which starts with C:/

在另一页我可以查看记录的内容。当我点击链接到其保存在C:/,铬说无法加载本地资源。我保存到资源文件夹这是我的ASP.NET应用程序的目录的一部分。无论如何围绕这吗?

On another page I can view the contents of the record. When I click on the link to which its saved on the C:/, Chrome says 'Failed to load local resource'. I am saving into the Resources folder which is part of my ASP.NET app directory. Anyway around this?

编辑
该页面从这个视图服务:

EDIT The page is served from this View:

public ActionResult Suggestions(String Tag)
{
      return View();
}

编辑2 - 我把我的看法的变化:

EDIT 2 - I put the changes in my view:

@{
string tag = "<td><a href=" + "~/Content/Files/" + ">" + item.Title.Replace(" ", "") + ".pdf" + "</a>" + "</td>";
 }
 @Html.Raw(tag)

在浏览器地址栏中的请求的文件

The requested file in the browser address bar is

http://localhost:62165/Incident/~/Content/Files/

现在我得到一个HTTP错误404.0 Not Found错误

Now I get a HTTP Error 404.0 Not Found error

推荐答案

尝试将文件保存在文件夹中的内容。您可以创建子文件内容/文件。把你的文件存在。于是产生这样的链接:

Try to save the files in Content folder. You can create a sub folder Content/Files. Put you files there. Then generate the links like:

&LT; A HREF =〜/内容/文件/ file1.pdf&GT;文件1&LT; / A&GT;

如果你想直接下载使用下载属性:

or use the download attribute if you want to download directly:

&LT; A HREF =〜/内容/文件/ file1.pdf下载&GT;文件1&LT; / A&GT;

大多数Web服务器被配置为拒绝对内容文件夹以外的内容请求。这可以在配置文件进行修改,但更容易(更安全),如果您使用的内容的文件夹。的这是根据我的经验,如果有人认为我错了,请让我知道来解决我的答案。我总是很高兴去学习新的东西。

Most web servers are configured to deny requests for content outside the content folder. This can be modified in the config file but is easier (and safer) if you use the content folder. This is based on my experience, if someone think that I'm wrong please let me know to fix my answer. I'm always glad to learn something new.

修改1:如何建立 A 标签动态

EDIT 1: How to build the a tag dynamically

在视图中,您需要一个字符串变量链接保存要显示链接的文本和另一个变量路径保存路径(也许两者都是从数据库加载到控制器)。然后,你可以手动生成标签是这样的:

In the view you need a string variable link that holds the text of the link you want to show and another variable path to hold the path (maybe both are loaded from db in the controller). Then you can build the tag manually like this:

@{
    string tag = "<a href=" + path + ">" + link + "</a>";
}

@Html.Raw(tag)

编辑2:在地址空间处理

您需要使用 Html.Content 来得到正确的相对URL。

You need to use Html.Content to get the right relative Url.

@{
    string link = item.Title;
    string path = Url.Content("~/Content/Files/") + link.Replace(" ", "%20") + ".pdf";
    string tag = "<a href=" + path + ">" + link + "</a>";
}

@Html.Raw(tag)

这篇关于上传和查看文件ASP.NET MVC 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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