Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view a file after uploading it to App_Data/Uploads in MVC 3 with Razor?

I am new to mvc and I am stuck with a problem. I searched all over for an answer and I couldn't find one, but I am very sure that something skipped me. The problem is that I don't know how to acces a file after I upload it to App_Data folder. I use the same code that I found on all forums:

For my view I use this

@using (Html.BeginForm("Index", "Home", FormMethod.Post, 
new { enctype="multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="submit" />
}

For my controller I use this

public class HomeController : Controller
{
// This action renders the form
public ActionResult Index()
{
    return View();
}

// This action handles the form POST and the upload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
    // Verify that the user selected a file
    if (file != null && file.ContentLength > 0) 
    {
        // extract only the fielname
        var fileName = Path.GetFileName(file.FileName);
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path);
    }
    // redirect back to the index action to show the form once again
    return RedirectToAction("Index");        
  }
} 

My model is

public class FileDescription
{
    public int FileDescriptionId { get; set; }
    public string Name { get; set; }
    public string WebPath { get; set; }
    public long Size { get; set; }
    public DateTime DateCreated { get; set; }
}

The thing is I want to upload a file to the database, and then the WebPath to be the link to my file. I hope I made myself clear enough. Any help would really be appreciated. Thanks

like image 851
Robert Avatar asked Sep 04 '11 21:09

Robert


People also ask

What is the use of App_Data folder in MVC?

The App_Data folder of MVC application is used to contain the application related data files like . mdf files, LocalDB, and XML files, etc. The most important point that you need to remember is that IIS is never going to serve files from this App_Data folder.

What is HttpPostedFileBase in MVC?

The HttpPostedFileBase class is an abstract class that contains the same members as the HttpPostedFile class. The HttpPostedFileBase class lets you create derived classes that are like the HttpPostedFile class, but that you can customize and that work outside the ASP.NET pipeline.

What is App_Data folder in asp net?

App_Data contains application data files including . mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information.


1 Answers

You can access your file server-side (so to access its contents from ASP.NET application) - just use Server.MapPath("~/App_Data/uploads/<yourFileName>") to get the absolute path (eg. C:/inetpub/wwwroot/MyApp/Add_Data/MyFile.txt).

Contents of App_Data folder are not directly accessible by URL for security reasons. All configs, databases are stored there so it's rather obvious why. If you need your uploaded file to be accessible via URL - upload it to some other folder.

I guess you need that file to be accessible via web. In such case the easiest solution would be to keep a filename (or full absolute path as mentioned at the beginning) to your file in a database and create a controller action that would take a filename and return the contents of your file.

like image 81
Piotr Szmyd Avatar answered Sep 19 '22 10:09

Piotr Szmyd