簡體   English   中英

在ASP.NET MVC 4架構注意事項中上載和處理CSV文件

[英]Upload and Process CSV File in ASP.NET MVC 4 Architectural Considerations

我正在開發一個導入和處理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文件夾,並在添加新內容時進行處理,但希望看到我在開始沿着這條路徑旅行之前從社區收到了什么輸入。

有沒有更有效的方法來處理此操作?

有沒有辦法執行此操作,允許用戶繼續他/她的快樂方式,然后在處理完成后通知用戶?

我遇到的問題的解決方案有點復雜,但與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控制器:

  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();
        }

進口加工指數:

@{
    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>

只是一個想法,但你可以線程處理你的CSV文件,並在完成該任務調用另一種方法,基本上提供模式對話或客戶端的某種javascript警報,讓用戶知道處理已完成。

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

或類似的規定。 我認為最終你會想要看某種線程,因為當某種服務器端處理發生時,用戶被卡住看屏幕肯定沒有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM