重命名上传的文件,以便可以将其删除 DropZone.js

Rename uploaded file so it can be removed DropZone.js

本文关键字:删除 DropZone js 文件 重命名      更新时间:2023-09-26

我决定更新这个问题,因为我仍然无法更改imageId

这是我当前的拖放区实现

   $("div#UploadImage").dropzone({
            url: '@Url.Action("SaveUploadedPhoto", "Listing", new { advertId = @Model.AdvertId })',
            addRemoveLinks: true,
            init: function () {
                var myDropzone = this;
                // First change the button to actually tell Dropzone to process the queue.
                $("input[type=submit]").on("click", function (e) {
                    // Make sure that the form isn't actually being sent.
                    e.preventDefault();
                    e.stopPropagation();
                    myDropzone.processQueue();
                });
                myDropzone.on("success", function (file, rep) {
                    console.log(rep.UniqueId);
                    fileList[i] = { "serverFileName": rep.UniqueId, "fileName": rep.UniqueId, "fileId": rep.UniqueId, "name": rep.UniqueId };
                    i++;
                    console.log(fileList);
                });
                myDropzone.on("removedfile", function (file) {
                    var rmvFile = "";
                    for (f = 0; f < fileList.length; f++) {
                        if (fileList[f].fileName == file.name) {
                            rmvFile = fileList[f].serverFileName;
                        }
                    }
                    if (rmvFile) {
                        $.ajax({
                            url: '@Url.Action("DeleteUploadedFile", "Listing")',
                            type: "POST",
                            data: { "id": rmvFile }
                        });
                    }
                });
            },
            maxFilesize: 2,
            maxFiles: 12
        });

当我上传图像时,我们将图像传递给第三方公司,该公司返回该图像的唯一 Id,然后这个唯一 ID 保存在我的数据库中,然后我将此 uniqueId 传递回视图,该视图将成为上传的图像名称,我正在尝试在上述 dropzone 实现中的"成功"函数中执行此操作, 当我这样做时

console.log(rep.UniqueId);

我可以看到该值及其正确性。

当我在拖放区上按"删除图像"时,会调用"已删除文件"函数,这不会向控制器触发,因为 rmvFile 是旧图像名称,并且在 for 循环中找不到匹配项,我知道这一点,因为当我这样做时

console.log(rmvFile);  

它显示了旧名称,现在我的问题是我在这里做错了什么? 如何将上传的镜像名称或 ID 更改为上传完成后返回的唯一 Id? 这样我就可以在需要时成功删除它。

我已经浏览了网络,尝试了情况,这导致了我的成功方法目前的样子。

在尝试了许多不同的解决方案之后,我现在已经设法让它工作了。

我发现在图像的唯一 ID 中执行以下传递

file.serverId['UniqueId'],