如何将它与剃刀上传到App_Data文件/上传在MVC 3后查看文件? [英] How to view a file after uploading it to App_Data/Uploads in MVC 3 with Razor?

查看:502
本文介绍了如何将它与剃刀上传到App_Data文件/上传在MVC 3后查看文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的MVC和我坚持的一个问题。我找遍了一个答案,我无法找到一个,但我非常肯定的东西跳过我。问题是,我不知道如何存取权限的文件我把它上传到App_Data文件夹后。我用我所有的论坛上发现了同样的代码:

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:

有关我的看法我用这个

@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; }
}



关键是我要上传文件到数据库,然后在WebPath是链接到我的文件。我希望我自己很清楚。任何帮助真的可以理解的。
谢谢

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

推荐答案

您可以访问您的文件服务器端(这样来访问ASP.NET应用程序的内容) - 只需使用使用Server.Mappath(〜/ App_Data文件/上传/< yourFileName>中)来得到的绝对路径(例如C:/的Inetpub / wwwroot文件/ MyApp的/ add_data工具/ MyFile.txt的)。

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).

App_Data文件夹中的内容不会被URL出于安全原因,直接访问即可。所有CONFIGS,数据库存储有那么这是相当明显的原因。如果您需要上传的文件是通过URL访问 - 上传到其他文件夹

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.

这篇关于如何将它与剃刀上传到App_Data文件/上传在MVC 3后查看文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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