Upload and View Files ASP.NET MVC 5

ASPCoder1450

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

rareyesdev

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Upload and View Files ASP.NET MVC 5

From Dev

How to upload video in ASP.NET MVC 5 and pass the video file from a view to a method?

From Dev

View an image in ASP.NET MVC 5

From Dev

Asp.net MVC: upload multiple image files?

From Dev

Upload multiple files with FormData using AngularJS and Asp.Net MVC

From Dev

Upload multiple files/ filepath using asp.net mvc

From Dev

How to upload files with additional data in ASP.NET MVC

From Dev

ASP.Net MVC 5 image upload to folder

From Dev

JS files are not linking to asp.net MVC view

From Dev

Use view models or DB models in ASP.NET MVC 5

From Dev

Unable to find Area View in ASP.Net MVC 5

From Dev

ASP.NET MVC 5 - redirect from view only once

From Dev

create editable cshtml View Page in asp.net mvc 5

From Dev

ASP.NET MVC 5 Model-Binding Edit View

From Dev

ASP .NET MVC 5 Filter get View.ViewBag data

From Dev

Remote validation not working for partial view in asp.net mvc 5

From Dev

Caching a Partial View Asp.net MVC 5

From Dev

Asp.net 5 mvc 6 get query string to view

From Dev

ASP .NET MVC 5 Write javascript in View.cshtml file

From Dev

how to edit in asp.net mvc 5 using view model?

From Dev

error on my view in asp.net mvc 5

From Dev

Access Generics List in ASP.NET MVC 5 View

From Dev

create grid view with devexpress asp.net mvc 5

From Dev

Controller for partial view in asp.NET 5 MVC

From Dev

Controller for partial view in asp.NET 5 MVC

From Dev

ASP.NET MVC 5 - redirect from view only once

From Dev

how to edit in asp.net mvc 5 using view model?

From Dev

create grid view with devexpress asp.net mvc 5

From Dev

create editable cshtml View Page in asp.net mvc 5

Related Related

HotTag

Archive