.Net Core 删除上传的文件

.Net Core Delete uploaded Files

我正在使用 ASP.NET Core 将文件上传到我的数据库。 我有两个数据库,一个用于创建,一个用于文件。 我的代码如下所示:

public async Task<IActionResult> Create([Bind("ID,Name,Email,Job Title,ICollection<IFormFile> uploads, Track track)
      {
           if (ModelState.IsValid)
            {
                _context.Add(track);

                // Uploading files for the Request Database
                foreach (var upload in uploads)
                {
                    if (upload.Length > 0)
                    {
                        // Getting file into buffer.
                        byte[] buffer = null;
                        using (var stream = upload.OpenReadStream())
                        {
                            buffer = new byte[stream.Length];
                            stream.Read(buffer, 0, (int)stream.Length);
                        }
                        // Converting buffer into base64 code.
                        string base64FileRepresentation = Convert.ToBase64String(buffer);
                        // Saving it into database.
                        _context.Upload.Add(new Request()
                        {
                            UploadName = string.Format("{0}_{1}", DateTime.UtcNow.ToString("yyyyMMddHHmmss"), Request.FileName),
                            Uploadcode = base64FileRepresentation,
                            TrackID = track.ID,
                        });
                        await _context.SaveChangesAsync();

                    }
                }
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(track);
        }

在编辑页面,我想要一个按钮来删除上传到轨道的文件。我试图将控制器的删除操作更改为以下代码,但没有成功:

    [HttpPost, ActionName("DeleteRequest")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteRequest(int id)
    {
        var x= await _context.Upload.SingleOrDefaultAsync(m => m.UploadID == id);
        _context.Upload.Remove(x);
        await _context.SaveChangesAsync();
        return RedirectToAction("Index");
    }

我的查看代码

<a asp-action="DeleteRequest"><span class="glyphicon glyphicon-trash" style="color:red;"></span></a>

当我点击它时,它会带我进入空白页面 URL: localhost:44444/Tracks/DeleteRequest

<a asp-action="DeleteRequest"   asp-controller="UpdatYourControllerName"
   asp-route-id="@model.UploadId"><span class="glyphicon glyphicon-trash" style="color:red;"></span></a>

public async Task<IActionResult> DeleteRequest(int id)
{
   // here debug the id you passed is actually present in db 
    var x= await _context.Upload.FirstOrDefaultAsync(m => m.UploadID  == id);
    if(x!=null)
    {
    _context.Upload.Remove(x);
    await _context.SaveChangesAsync();
    return RedirectToAction("Index");
    }
}