0

这是当前代码:JS:

var files = new Array();

function () {
                var me = this;

                for (var i = 0; i < files.length; i++) {
                    var formData = new FormData();

                    formData.append('files', files[i]);
                    formData.append('folderId', (Ext.ComponentQuery.query('#treePanel')[0]).getSelectionModel().getSelection()[0].data.id);

                    var xhr = new XMLHttpRequest();

                    xhr.open('post', '/File/Upload', true);
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState == 4 && xhr.status == 200) {
                            me.getUserSessionStore().load();
                            me.getFilesStore().load();
                        }
                    };
                    xhr.send(formData);
                }

}

和 C#:

[Authorize]
    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase files, long folderId)
    {
        var user = new UsersModel().GetUserByEmail(User.Identity.Name);

        if (files != null)
        {
            string x = Path.GetExtension(files.FileName);

            if (new FilesModel().GetFileTypes().ToList().Where(a => a.ftp_extension == x.TrimStart('.')).Count() != 0)
            {
                var dbPath = new FilesModel().GetPaths().First();
                var fileName = Path.GetFileNameWithoutExtension(files.FileName);
                var fileType = new FilesModel().GetFileTypes().ToList().First(a => a.ftp_extension == x.TrimStart('.'));
                Directory.CreateDirectory(Server.MapPath("/Temp/") + user.usr_id);
                var path = Path.Combine(Server.MapPath("/Temp/") + user.usr_id + "/", fileName);
                files.SaveAs(path);
                Directory.CreateDirectory(dbPath.pth_path + "/tmp/" + user.usr_id + "/");
                if (System.IO.File.Exists(Server.MapPath("/Temp/" + user.usr_id + "/" + fileName)))
                    System.IO.File.Move(Server.MapPath("/Temp/" + user.usr_id + "/" + fileName), dbPath.pth_path + "tmp/" + user.usr_id + "/" + fileName);

                var fileId = new FilesModel().CreateFile(user.usr_id, fileName, false, "", fileName, 1, fileType.ftp_id, user.usr_id, folderId);

                if (System.IO.File.Exists(dbPath.pth_path + "tmp/" + user.usr_id + "/" + fileName))
                    System.IO.File.Move(dbPath.pth_path + "tmp/" + user.usr_id + "/" + fileName, dbPath.pth_path + new Pass().getMd5Hash(fileId.ToString()));
                return Content("{success:true, result:\"File uploaded correctly\"}");
            }
            else
            {
                return Content("{success:false, error:\"Unsupported file type\"}");
            }
        }
        return new JsonResult()
        {
            ContentType = "text/html",
            Data = new { success = false, error = "File uploaded error" }
        };
    }
}

如何制作一个进度条来表示服务器端的上传进度?我如何获得SaveAs(如果可能的话)的上传进度,如果不是我必须改变什么?

提前致谢 !

4

1 回答 1

0

当您在那里迭代您的文件列表时,请在客户端解决它。从脚本更新进度条客户端应该是微不足道的。

从服务器上的活动更新进度条需要从服务器到客户端的回调(您可以使用 SignalR)或从客户端轮询。这两种选择都需要大量的工作才能做到正确。

我觉得你不是一个经验丰富的程序员,所以除非这是你自己的宠物项目,否则我会选择第一个选项。

于 2012-10-10T10:11:21.223 回答