1

我正在使用 Kendo UI 上传器。当我使用 kendoUpload 上传文件时,实际上我在服务器端使用 Guid.NewGuid() 重命名文件。问题是,当我想删除文件时,原始文件名被发送到服务器端的删除处理程序而不是 guidName。我该如何解决这个问题?

我在服务器端的删除处理程序如下:

[HttpPost]
public ActionResult RemoveTemp(string[] fileNames)
{
    List<string> removedFiles = new List<string>();
    string tempPath = Server.MapPath("~/temp/");
    if (fileNames != null)
    {
        foreach (var fullName in fileNames)
        {
            File.Delete(tempPath + fullName);

            removedFiles.Add(fullName);
        }
    }

    return Json(removedFiles.ToArray());
}

我在客户端的删除事件如下:

    remove: function (e) {
                    var fileToRemove = e.files[0].name;

                    for (var i = 0; i < vm[item].length; i++) {
                        if (vm[item][i].originalName == fileToRemove) {
                            vm[item].splice(i, 1);

                            break;
                        }
                    }

// I don't know how to send guidNames here using e.data
                }
4

1 回答 1

2

您需要在上传响应中包含已保存文件的名称,并在客户端设置相应的名称e.files

示例上传操作:

    [HttpPost]
    public ActionResult UploadFiles()
    {
        // Note: We use Request.Files instead of a parameter input, to be independent of the name of the Kendo upload component
        var count = Request.Files.Count;
        if (count == 0)
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

        var result = new List<UploadedFile>(count);
        for (var i = 0; i < count; i++)
        {
            HttpPostedFileBase file = Request.Files[i];
            if (file == null || (file.ContentLength == 0 && string.IsNullOrEmpty(file.FileName)))
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            // Some browsers send file names with full path. We are only interested in the file name.
            var fileName = Path.GetFileName(file.FileName);

            var tempKey = _svcUpload.SaveTempFile(file.InputStream, fileName);
            result.Add(new UploadedFile
            {
                TempKey = tempKey,
                Name = fileName,
                Extension = Path.GetExtension(file.FileName),
                Size = file.ContentLength
            });
        }

        return Json(result);
    }

_svcUpload.SaveTempFile()保存上传的文件并返回其临时密钥(可以是重命名文件的 GUID)。我们在响应中包含临时键以及其他文件信息。

这是客户端上传成功处理程序:

function fileUploadSuccess(e) {
    if (e.operation === 'upload') {
        for (var i = 0; i < e.response.length; i++) {
            var tempKey = e.response[i].TempKey;
            e.files[i].name = tempKey;
        }
    }
}
于 2018-01-08T03:53:57.360 回答