从服务器删除上传的文件

时间:2012-03-05 15:47:08

标签: c# javascript jquery asp.net-mvc uploadify

我在ASP.NET MVC应用程序中使用Uploadify。文件上传后,我们看到: enter image description here

如果用户点击取消/交叉按钮,是否可以删除上传的文件(来自服务器)?任何建议/指针都会非常有用。

1 个答案:

答案 0 :(得分:2)

一种可能性是,一旦上传完成,您的服务器端上传操作将返回唯一ID,以便您稍后识别服务器上的文件:

[HttpPost]
public ActionResult Upload()
{
    var fileId = Guid.NewGuid().ToString();

    // TODO: do the uploading ...

    return Json(new { id = fileId });
}

并在客户端上保留服务器返回的唯一文件ID与客户端上的ID之间的映射。然后,您可以订阅onCompleteonCancel事件:

$(function () {
    var map = {};
    $('#file_upload').uploadify({
        'uploader': '@Url.Content("~/scripts/uploadify/uploadify.swf")',
        'script': '@Url.Action("upload")',
        'cancelImg': '@Url.Content("~/scripts/uploadify/cancel.png")',
        'auto': true,
        'removeCompleted': false,
        'multi': true,
        'onComplete': function (event, ID, fileObj, response, data) {
            // once the upload succeeds we retrieve the id returned 
            // by the server and we put it in the map
            map[ID] = $.parseJSON(response).id;
        },
        'onCancel': function (event, ID, fileObj, data) {
            var fileId = map[ID];
            if (fileId) {
                // TODO: here you could send a request to the server to 
                // inform him that the user wants to delete the file with 
                // the given unique id
            }
        }
    });
});