asp.net-mvc - FilePond如何将删除的文件的名称或id作为参数传递给 Controller ​​?

标签 asp.net-mvc file-upload filepond

我正在使用 filepond 插件批量上传文件。上传的文件保存到缓存中。现在我想点击删除按钮来删除我的缓存中相应的文件。我想知道前端文件的名称或id应该如何传输到 Controller ? 或者说 Controller 如何在表单提交时获取filepond文件,也能解决我现在的问题。

这是我使用的c#代码:

    [HttpPost]
    public ActionResult SaveFiles()
    {
        if (Request.Files.Count > 0)
        {
            var file = Request.Files[0];
            if (file != null && file.ContentLength > 0)
            {
                UploadedFiles uploadedFiles = new UploadedFiles()
                {
                    UserName = UserSession.CurrentUser.UserName,
                    PostedFile = file
                };
                uploadedFilesList.Add(uploadedFiles);
            }
        }
        return Json(true);
    }

    [HttpDelete]
    public ActionResult RemoveFile()
    {
       // How to put the id or name of the deleted file into this action?
        return Json(true);
    }

这是我使用的html和javascript代码

<div class="controls">
    <input type="file" class="filepond" name="filepond" multiple
           data-max-file-size="50MB" data-max-files="10">
    <p class="help-block">Optional.</p>
</div>

<link href="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css" rel="stylesheet" />
    <link href="https://unpkg.com/filepond/dist/filepond.min.css" rel="stylesheet">
    <link href="https://unpkg.com/filepond-plugin-file-poster/dist/filepond-plugin-file-poster.css" rel="stylesheet">
    <script src="https://unpkg.com/filepond-plugin-file-encode/dist/filepond-plugin-file-encode.min.js"></script>
    <script src="https://unpkg.com/filepond-plugin-file-validate-size/dist/filepond-plugin-file-validate-size.min.js"></script>
    <script src="https://unpkg.com/filepond-plugin-image-exif-orientation/dist/filepond-plugin-image-exif-orientation.min.js"></script>
    <script src="https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js"></script>
    <script src="https://unpkg.com/filepond/dist/filepond.min.js"></script>
<script>
FilePond.registerPlugin(
  FilePondPluginImagePreview,
  FilePondPluginImageExifOrientation,
  FilePondPluginFileValidateSize
);
    // Select the file input and use create() to turn it into a pond
   FilePond.create(document.querySelector('.filepond'));

FilePond.setOptions({
    server: {
        process: '/List/SaveFiles',
        revert: '/List/RemoveFile',
        
    }
});
</script>

最佳答案

通常,如果您不更改 Filepond 恢复 设置中的任何内容,文件 ID 将以简单文本形式在请求正文中发送。要获取此 id,您需要执行以下操作:

public async Task<IActionResult> OnDelete()
{
    var fileId = "";
    using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
    {
        fileId = await reader.ReadToEndAsync();
    }

    bool isDeleted = false;

    // Do delete your file

    if (isDeleted)
        return new ContentResult() { Content = "deleted" };
    else
        return new ContentResult() { Content = "error", StatusCode = 500 };
}

这是来自 RazorPage 的 ASP.NET Core 版本

关于asp.net-mvc - FilePond如何将删除的文件的名称或id作为参数传递给 Controller ​​?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343437/

相关文章:

laravel - Filepond 与 livewire 的集成不会以 1% 到 100% 等百分比显示进度条

filepond - 如何在 filepond 中获取还原文件的原始属性

asp.net-mvc - 在 ASPNET MVC 中通过身份验证提供 iCalendar 文件

asp.net-mvc - 如何在 ASP.Net MVC 中对自定义 ActionFilter 进行单元测试

html - 在我的表单上排列一些对象时遇到问题

java - 通过okhttp3上传文件

javascript - Multer 不上传文件

javascript - 如何在 JS 中从 ArrayBuffer 写入文件

javascript - MVC 5 ASP.Net FORM 提交()函数不工作

javascript - 如何将 FilePond 指向 django 媒体文件