I have this 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();
}
The link is the filepath which is stored in the DB which starts with C:/
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();
}
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/
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:
<a href="~/Content/Files/file1.pdf">File1</a>
or use the download attribute if you want to download directly:
<a href="~/Content/Files/file1.pdf" download>File1</a>
.
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.
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)
EDIT 2: Dealing with spaces in 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)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments