允许用户仅查看他们上传的数据 (Asp.NET MVC5)
Allow user to see only their uploaded data (Asp.NET MVC5)
我可以上传和下载上传的文件,但我希望用户只能看到他们的文件。 (user1 只能看到他的数据,而不能看到 user2 的数据)我该怎么做?任何想法都可以提供帮助。谢谢!
这是我的控制器,我知道我可以使用授权限制管理员、用户、编辑之间的访问,但这无助于限制 user_id 之间的访问。
(需要说明隐私是我项目的一个非常重要的方面)
public class FileUploadController : Controller
{
// GET: FileUpload
public ActionResult Index()
{
var items = GetFiles();
return View(items);
}
// POST: FileUpload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if(file != null && file.ContentLength > 0 )
try
{
string path = Path.Combine(Server.MapPath("~/Files"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
}
catch(Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
var items = GetFiles();
return View(items);
}
public FileResult Download(string downloadedfile)
{
var FileVirtualPath = "~/Files/" + downloadedfile;
return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
}
private List <string> GetFiles()
{
var dir = new System.IO.DirectoryInfo(Server.MapPath("~/Files"));
System.IO.FileInfo[] fileNames = dir.GetFiles("*.*");
List<string> items = new List<string>();
foreach (var file in fileNames)
{
items.Add(file.Name);
}
return items;
}
}
这是观点:
<h2> File Upload </h2>
@model List<string>
@using (Html.BeginForm("Index", "FileUpload", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<label for="file"> Upload </label>
<input type="file" name="file" id="file" />
<br /><br />
<input type="submit" value="Upload" />
<br /><br />
@ViewBag.Message
<br/>
<h2>Documents list</h2>
<table style="width:100%">
<tr>
<th> File Name </th>
<th> Link </th>
</tr>
@for (var i = 0; i <= (Model.Count)-1 ; i++)
{
<tr>
<td>@Model[i].ToString() </td>
<td>@Html.ActionLink("Download", "Download", new { downloadedfile = Model[i].ToString() }) </td>
</tr>
}
</table>
}
<style>
table, th, td {
border: 1px solid white;
}
</style>
可以分离到文件夹,以用户id创建文件夹,将数据放在用户文件夹中。
列出时,只显示用户的文件夹
我可以上传和下载上传的文件,但我希望用户只能看到他们的文件。 (user1 只能看到他的数据,而不能看到 user2 的数据)我该怎么做?任何想法都可以提供帮助。谢谢!
这是我的控制器,我知道我可以使用授权限制管理员、用户、编辑之间的访问,但这无助于限制 user_id 之间的访问。
(需要说明隐私是我项目的一个非常重要的方面)
public class FileUploadController : Controller
{
// GET: FileUpload
public ActionResult Index()
{
var items = GetFiles();
return View(items);
}
// POST: FileUpload
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if(file != null && file.ContentLength > 0 )
try
{
string path = Path.Combine(Server.MapPath("~/Files"),
Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
}
catch(Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
var items = GetFiles();
return View(items);
}
public FileResult Download(string downloadedfile)
{
var FileVirtualPath = "~/Files/" + downloadedfile;
return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
}
private List <string> GetFiles()
{
var dir = new System.IO.DirectoryInfo(Server.MapPath("~/Files"));
System.IO.FileInfo[] fileNames = dir.GetFiles("*.*");
List<string> items = new List<string>();
foreach (var file in fileNames)
{
items.Add(file.Name);
}
return items;
}
}
这是观点:
<h2> File Upload </h2>
@model List<string>
@using (Html.BeginForm("Index", "FileUpload", FormMethod.Post,
new { enctype = "multipart/form-data" }))
{
<label for="file"> Upload </label>
<input type="file" name="file" id="file" />
<br /><br />
<input type="submit" value="Upload" />
<br /><br />
@ViewBag.Message
<br/>
<h2>Documents list</h2>
<table style="width:100%">
<tr>
<th> File Name </th>
<th> Link </th>
</tr>
@for (var i = 0; i <= (Model.Count)-1 ; i++)
{
<tr>
<td>@Model[i].ToString() </td>
<td>@Html.ActionLink("Download", "Download", new { downloadedfile = Model[i].ToString() }) </td>
</tr>
}
</table>
}
<style>
table, th, td {
border: 1px solid white;
}
</style>
可以分离到文件夹,以用户id创建文件夹,将数据放在用户文件夹中。
列出时,只显示用户的文件夹