1

我正在使用 filepond 插件批量上传文件。上传的文件被保存到缓存中。现在我想点击删除按钮来删除我缓存中的相应文件。我想知道前端文件的名称或id应该如何传输到控制器?或者控制器在表单提交时如何获取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>

4

2 回答 2

2

通常,如果您不更改 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 版本

于 2019-11-29T12:25:03.560 回答
1

您不应该使用 ActionResult。

    [HttpDelete]
    public string RemoveFile(int id)
    {
       return "Deleted!";
    }
于 2019-10-11T14:43:19.887 回答