ASP*_*450 10 c# asp.net asp.net-mvc
我有这个代码:
[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();
}
Run Code Online (Sandbox Code Playgroud)
链接是存储在以C:/ C开头的DB中的文件路径
在另一个页面上,我可以查看记录的内容.当我点击其保存在C:/上的链接时,Chrome会显示"无法加载本地资源".我保存到Resources文件夹中,该文件夹是我的ASP.NET应用程序目录的一部分.无论如何围绕这个?
编辑该页面由此视图提供:
public ActionResult Suggestions(String Tag)
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
编辑2 - 我把更改放在我的视图中:
@{
string tag = "<td><a href=" + "~/Content/Files/" + ">" + item.Title.Replace(" ", "") + ".pdf" + "</a>" + "</td>";
}
@Html.Raw(tag)
Run Code Online (Sandbox Code Playgroud)
浏览器地址栏中请求的文件是
http://localhost:62165/Incident/~/Content/Files/
Run Code Online (Sandbox Code Playgroud)
现在我收到HTTP错误404.0 Not Found错误
rar*_*dev 19
这是一个完整的示例,显示如何上传文件并通过链接将其下载.
创建一个空的MVC项目. 我使用MVC 4,但它应该与MVC 5一起使用.
控制器:
在HomeController我们将有一个动作Index,将显示可供下载的文件列表和上传新文件的选项.
行动IndexGET:
Index视图的模型.动作IndexPOST:
码:
public class HomeController : Controller
{
public ActionResult Index()
{
var path = Server.MapPath("~/Content/Files/");
var dir = new DirectoryInfo(path);
var files = dir.EnumerateFiles().Select(f => f.Name);
return View(files);
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
var path = Path.Combine(Server.MapPath("~/Content/Files/"), file.FileName);
var data = new byte[file.ContentLength];
file.InputStream.Read(data, 0, file.ContentLength);
using (var sw = new FileStream(path, FileMode.Create))
{
sw.Write(data, 0, data.Length);
}
return RedirectToAction("Index");
}
}
Run Code Online (Sandbox Code Playgroud)
视图:
在视图中,我们需要生成一个包含文件链接的列表.这里我们需要处理包含空格的文件名,并用'%20'替换它们.
上传文件的表单很简单.只是一个输入标签来获取文件和一个按钮来发送表单.
@model IEnumerable<string>
@{
ViewBag.Title = "Index";
}
<h2>Files</h2>
<ul>
@foreach (var fName in Model)
{
var name = fName;
var link = @Url.Content("~/Content/Files/") + name.Replace(" ", "%20");
<li>
<a href="@link">@name</a>
</li>
}
</ul>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="File" name="file" id="file" value="Choose File"/>
<button type="submit">Upload</button>
}
</div>
Run Code Online (Sandbox Code Playgroud)
结果应该是:

尝试将文件保存在 Content 文件夹中。您可以创建子文件夹 Content/Files。把你的文件放在那里。然后生成如下链接:
<a href="~/Content/Files/file1.pdf">File1</a>
或者如果您想直接下载,请使用 download 属性:
<a href="~/Content/Files/file1.pdf" download>File1</a>。
大多数 Web 服务器都配置为拒绝对内容文件夹之外的内容的请求。这可以在配置文件中修改,但如果您使用内容文件夹会更容易(也更安全)。这是基于我的经验,如果有人认为我错了,请让我知道我的答案。我总是很高兴学习新东西。
编辑1:如何a动态构建标签
在视图中,您需要一个字符串变量link来保存要显示的链接的文本,另一个变量path来保存路径(可能两者都是从控制器中的数据库加载的)。然后您可以像这样手动构建标签:
@{
string tag = "<a href=" + path + ">" + link + "</a>";
}
@Html.Raw(tag)
Run Code Online (Sandbox Code Playgroud)
编辑 2:处理 Url 中的空格。
您需要使用Html.Content来获取正确的相对 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38729 次 |
| 最近记录: |