在ASP.NET MVC 4架构注意事项中上载和处理CSV文件

时间:2012-12-04 19:15:20

标签: c# asp.net-mvc file-upload

我正在开发一个导入和处理CSV文件的ASP.NET MVC 4应用程序。我正在使用标准表单和控制器进行上传。以下是我目前正在做的事情的概述:

控制器逻辑

public ActionResult ImportRecords(HttpPostedFileBase importFile){

    var fp = Path.Combine(HttpContext.Server.MapPath("~/ImportUploads"), Path.GetFileName(uploadFile.FileName));
    uploadFile.SaveAs(fp);

    var fileIn = new FileInfo(fp);
    var reader = fileIn.OpenText();
     var tfp = new TextFieldParser(reader) {TextFieldType = FieldType.Delimited, Delimiters = new[] {","}};
    while(!tfp.EndOfData){
        //Parse records into domain object and save to database
    }
    ...
}

HTML

@using (Html.BeginForm("ImportRecords", "Import", FormMethod.Post, new { @id = "upldFrm", @enctype = "multipart/form-data" }))
{
    <input id="uploadFile" name="uploadFile" type="file" />
    <input id="subButton" type="submit" value="UploadFile" title="Upload File" />
}

导入文件可能包含大量记录(平均40K +),可能需要相当长的时间才能完成。对于处理的每个文件,我不希望用户在导入屏幕上坐5分钟以上。我考虑过添加一个控制台应用程序来监视新文件的uploads文件夹,并在添加新内容时进行处理,但希望看到我在开始沿着这条路径旅行之前从社区收到了什么输入。

是否有更有效的方法来处理此操作?

有没有办法执行此操作,允许用户继续他/她的快乐方式,然后在处理完成后通知用户?

2 个答案:

答案 0 :(得分:11)

我遇到的问题的解决方案有点复杂,但与IFrame修复工作类似。结果是一个处理处理的弹出窗口,允许用户继续在整个站点中导航。

将文件提交给服务器(UploadCSV控制器),返回带有一些JavaScript的Success页面,以处理处理的初始启动。当用户单击“开始处理”时,将打开一个新窗口(ImportProcessing / Index),该窗口将加载初始状态(启动检索状态更新的间隔循环),然后调用“StartProcessing”操作,启动处理过程。

我正在使用的“FileProcessor”类位于ImportProcessing控制器中的静态dictionairy变量中;允许基于密钥的状态结果。操作完成或遇到错误后立即删除FileProcessor。

上传控制器:

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UploadCSV(HttpPostedFileBase uploadFile)
        {
            var filePath = string.Empty;
            if (uploadFile.ContentLength <= 0)
            {
                return View();
            }
                filePath  = Path.Combine(Server.MapPath(this.UploadPath), "DeptartmentName",Path.GetFileName(uploadFile.FileName));
            if (new FileInfo(filePath).Exists)
            {
                ViewBag.ErrorMessage =
                    "The file currently exists on the server.  Please rename the file you are trying to upload, delete the file from the server," +
                    "or contact IT if you are unsure of what to do.";
                return View();
            }
            else
            {
                uploadFile.SaveAs(filePath);
                return RedirectToAction("UploadSuccess", new {fileName = uploadFile.FileName, processType = "sonar"});
            }
        }

 [HttpGet]
        public ActionResult UploadSuccess(string fileName, string processType)
        {
            ViewBag.FileName = fileName;
            ViewBag.PType = processType;
            return View();
        }

上传成功HTML:

@{
    ViewBag.Title = "UploadSuccess";
}

<h2>File was uploaded successfully</h2>
<p>Your file was uploaded to the server and is now ready to be processed.  To begin processing this file, click the "Process File" button below.
</p>
<button id="beginProcess" >Process File</button>
<script type="text/javascript">
    $(function () {
        $("#beginProcess").click(BeginProcess);
        function BeginProcess() {
            window.open("/SomeController/ImportProcessing/Index?fileName=@ViewBag.FileName&type=@ViewBag.PType", "ProcessStatusWin", "width=400, height=250, status=0, toolbar=0,  scrollbars=0, resizable=0");
            window.location = "/Department/Import/Index";
        }
    });
</script>

打开此新窗口后,文件处理开始。从自定义FileProcessing类中检索更新。

ImportProcessing Controller:

  public ActionResult Index(string fileName, string type)
        {
            ViewBag.File = fileName;
            ViewBag.PType = type;
            switch (type)
            {
                case "somematch":
                    if (!_fileProcessors.ContainsKey(fileName)) _fileProcessors.Add(fileName, new SonarCsvProcessor(Path.Combine(Server.MapPath(this.UploadPath), "DepartmentName", fileName), true));
                    break;
                default:
                    break;
            }
            return PartialView();
        }

ImportProcessing Index:

@{
    ViewBag.Title = "File Processing Status";
}
@Scripts.Render("~/Scripts/jquery-1.8.2.js")

<div id="StatusWrapper">
    <div id="statusWrap"></div>
</div>
<script type="text/javascript">
    $(function () {
        $.ajax({
            url: "GetStatusPage",
            data: { fileName: "@ViewBag.File" },
            type: "GET",
            success: StartStatusProcess,
            error: function () {
                $("#statusWrap").html("<h3>Unable to load status checker</h3>");
            }
        });
        function StartStatusProcess(result) {
            $("#statusWrap").html(result);
            $.ajax({
                url: "StartProcessing",
                data: { fileName: "@ViewBag.File" },
                type: "GET",
                success: function (data) {
                    var messag = 'Processing complete!\n Added ' + data.CurrentRecord + ' of ' + data.TotalRecords + " records in " + data.ElapsedTime + " seconds";
                    $("#statusWrap #message").html(messag);
                    $("#statusWrap #progressBar").attr({ value: 100, max: 100 });
                    setTimeout(function () {
                        window.close();
                    }, 5000);
                },
                error: function (xhr, status) {
                    alert("Error processing file");
                }
            });
        }
    });
</script>

最后是状态检查器html:

@{
    ViewBag.Title = "GetStatusPage";
}
<h2>Current Processing Status</h2>
    <h5>Processing: @ViewBag.File</h5>
    <h5>Updated: <span id="processUpdated"></span></h5>
    <span id="message"></span>
    <br />
    <progress id="progressBar"></progress>
<script type="text/javascript">
    $(function () {
        var checker = undefined;
        GetStatus();
        function GetStatus() {
            if (checker == undefined) {
                checker = setInterval(GetStatus, 3000);
            }
            $.ajax({
                url: "GetStatus?fileName=@ViewBag.File",
                type: "GET",
                success: function (result) {
                    result = result || {
                        Available: false,
                        Status: {
                            TotalRecords: -1,
                            CurrentRecord: -1,
                            ElapsedTime: -1,
                            Message: "No status data returned"
                        }
                    };
                    if (result.Available == true) {
                        $("#progressBar").attr({ max: result.Status.TotalRecords, value: result.Status.CurrentRecord });
                        $("#processUpdated").text(result.Status.Updated);
                        $("#message").text(result.Status.Message);
                    } else {
                        clearInterval(checker);
                    }

                },
                error: function () {
                    $("#statusWrap").html("<h3>Unable to load status checker</h3>");
                    clearInterval(checker);
                }
            });
        }
    });
</script>

答案 1 :(得分:0)

只是一个想法,但你可以通过线程处理你的CSV文件,并在完成该任务时调用另一种方法,它基本上在客户端提供模态对话或某种javascript警报,让用户知道处理已完成

Task.Factory.StartNew(() => ProcessCsvFile(fp)).ContinueWith((x) => NotifyUser());

或类似的东西。我认为最终你会想要看某种线程,因为当用户在进行某种服务器端处理时被卡在屏幕上看起来没有意义。