如何删除 mvc5 中的特定上传文件?
How can I delete a specific uploaded file in mvc5?
上传文件后,我希望能够将其删除。我尝试这样做,但出现错误“找不到资源”。另外,如何为行中的每个文件制作编辑按钮,以便上传该特定文件的更好版本?有人可以帮助我吗?
这是我的控制器:
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;
}
[HttpPost]
public ActionResult Delete(string file)
{
file = Path.Combine(Server.MapPath("~/Files"), file);
FileInfo fl = new FileInfo(file);
if (fl != null)
{
System.IO.File.Delete(file);
fl.Delete();
}
return View();
}
}
}
这是视图,唯一的问题是删除部分:
<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>
<td>@Html.ActionLink("Delete", "Delete", new { deletedfile = Model[i].ToString() }) </td>
</tr>
}
</table>
}
在您post删除方法的表单中,没有任何内容表明要删除的文件的路径是什么。您确实有一个用于计数 属性 的隐藏输入,但如果那是一个名为 "file" 的隐藏输入并且包含要删除的文件的路径值,那么它将被 posted .
请注意,出于安全原因,您可能不想让人们只 post 任何文件的路径,服务器将删除它。那会给你带来很多麻烦。您可能应该使用文件 url 和 ID 存储数据库记录,并将该 ID post 发送到服务器,然后从数据库中读取 URL,然后删除文件。
上传文件后,我希望能够将其删除。我尝试这样做,但出现错误“找不到资源”。另外,如何为行中的每个文件制作编辑按钮,以便上传该特定文件的更好版本?有人可以帮助我吗?
这是我的控制器:
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;
}
[HttpPost]
public ActionResult Delete(string file)
{
file = Path.Combine(Server.MapPath("~/Files"), file);
FileInfo fl = new FileInfo(file);
if (fl != null)
{
System.IO.File.Delete(file);
fl.Delete();
}
return View();
}
}
}
这是视图,唯一的问题是删除部分:
<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>
<td>@Html.ActionLink("Delete", "Delete", new { deletedfile = Model[i].ToString() }) </td>
</tr>
}
</table>
}
在您post删除方法的表单中,没有任何内容表明要删除的文件的路径是什么。您确实有一个用于计数 属性 的隐藏输入,但如果那是一个名为 "file" 的隐藏输入并且包含要删除的文件的路径值,那么它将被 posted .
请注意,出于安全原因,您可能不想让人们只 post 任何文件的路径,服务器将删除它。那会给你带来很多麻烦。您可能应该使用文件 url 和 ID 存储数据库记录,并将该 ID post 发送到服务器,然后从数据库中读取 URL,然后删除文件。