c# - ASP.NET MVC4网页异步FTP上传

标签 c# asp.net asp.net-mvc asp.net-mvc-4 ftp

我的问题与 Monker 在此处发布的问题完全相同:background file uploader?

在网页上总结问题:

  • 用户点击上传文件
  • 用户选择一个大文件上传
  • 用户点击“上传”按钮
  • 应用程序显示“正在上传您的文件。上传完成后您会收到通知”
  • 用户继续正常使用该应用程序,或者可能出现另一个窗口或灯箱显示进度条。

为什么我又在这里发帖?

  • 我的声誉不允许我对该帖子发表评论或回答。
  • 我想向您展示我用代码尝试过的所有事情
  • 该帖子还没有一个好的答案。

一些用户建议我们使用像 Telerik 这样的第 3 部分组件,但使用演示我们可以看到它在文件上传时卡住了页面。我已经用很多 ftp 组件进行了概念验证,现在我正在使用一个名为 ChlikatDotNet4 的组件,它允许我们启动异步文件上传方法,这似乎可以解决问题,但事实并非如此就这么简单,我会告诉你原因。

我使用 ChilkatDotNet4.dll 在 ASP.NET MVC 4 中制作了一个简单的 ftp 上传 Web 应用程序,它访问托管在我的工作组网络中的 ftp 服务器。这是我的索引页面中的代码,其中包含加载文件的输入和提交按钮:

@using (Html.BeginForm("TesteUpload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
        <tr>
            <td>
                <label>Arquivo: </label>
            </td>
            <td>
                <input type="file" id="PathArquivo" name="PathArquivo"/>
            </td>
        </tr>
        <br/>
        <br/>
</table>
<input type="submit" id="submit_upload" name="submit_upload" value="UPLOAD" />
}

在 Controller 上:

public ActionResult TesteUpload(HttpPostedFileBase PathArquivo)
{

    Ftp2 ftp = new Ftp2();

    //Here goes the hided code to set up the connection parameters to the FTP Server

    if (ftp.Connect())
        {

            //Start the asynchronous method 
            bool sucesso = ftp.AsyncPutFileStart(newName, remotePath + "/" + Path.GetFileName(PathArquivo.FileName));

            //Separate thread for long running operation
            if (sucesso)
            {
                ThreadPool.QueueUserWorkItem(delegate
                {
                    while (!ftp.AsyncFinished)
                    {
                        //Get percent of file uploading 
                        ulong bytesTotal = (ulong)st.Length;
                        ulong bytesSent = (ftp.AsyncBytesSent > 0 ? ftp.AsyncBytesSent : 1);

                        //Separated class with a static int to hold the value
                        Progresso.progresso = (int)((bytesSent * 100) / bytesTotal);
                    }

                    if (ftp.AsyncFinished)
                    {
                        Progresso.progresso = 100;
                    }

                });                    
            }

            ftp.Disconnect();


        }

    return PartialView("TesteDownload");
}

//Action to get the current progress
[NoCache]
public ActionResult StatusUpload()
{
    int operationProgress = Progresso.progresso;

    return Json(new { progress = operationProgress }, JsonRequestBehavior.AllowGet);
}

关于TesteDownload.cshtml源代码:

<div>

    <label id="progressLabel"></label><label>%</label>

    <div id="progressTimer"></div>

</div>



<script type="text/javascript">

    past = null;

    $.updateProgress = function () {
        //Getting current operation progress
        $.get('@Url.Action("StatusUpload")', function(data) {

            //Updating progress
            $("#progressLabel").text(data.progress);

            //If operation is complete
            if (data.progress >= 100) {
                $("#progressbarLabel").text(100);
            }
            //If not
            else {
                //Reset timer and call me again
                setTimeout($.updateProgress, 500);
            }

            //Code to show a counter with Hour, minutes, seconds and milliseconds:
            var now = new Date().getTime();
            var used = now - window.past.getTime();

            var milliseconds = used % 1000;
            var seconds = Math.floor((used / 1000) % 60);
            var minutes = Math.floor((used / (60 * 1000)) % 60);
            var hours = Math.floor((used / (60 * (60 * 1000))) % 60);

            $("#progressTimer").text(hours + ":" + minutes + ":" + seconds + "." + milliseconds);

        });
    };

    $(document).ready(function () {
        window.past = new Date();
        $("#progressLabel").text(0);
        $.updateProgress();
    });


</script>

我必须向您报告这个应用程序在本地运行成功,这意味着在选择一个文件并按下上传按钮后,我们可以立即看到 TesteDownload.cshtml 页面和计数器工作随着上传百分比的增长。当我在不在我机器上的服务器上发布此应用程序时,页面在单击后卡住,而另一个页面仅在文件传输完成后出现。

我想知道到底发生了什么,我是说。异步方法真的适用于服务器上的 ajax 吗?本地主机和不允许此功能的服务器之间有区别吗?

从现在开始,我感谢任何小小的帮助。

[]秒 里奥

最佳答案

我认为您在这里混淆了一些想法。因此,回答您的直接问题:是的,$.get 是真正的 ajax,您可以使用浏览器扩展程序(如 Firefox 中的 Firebug 或 IE 中的 DevTools)深入研究它。但是:你没有这样做,AJAX 应该完成。您有 2 个循环:1 个循环在客户端,另一个在服务器上。

您的服务器循环并没有真正连接到您的客户端循环,因此我认为结果很简单取决于。例如,如果您在本地使用 Visual Studio 中的 IISExpress(您只需按 F5),那么您将体验到与在 Web 上的真实 IIS 计算机上不同的行为。

StatusUpload 是另一个问题,因为您将以类似轮询的方式调用它,而且您还必须在这里处理阻塞的线程。它实际上取决于 100 多个其他因素,例如服务器上的应用程序池配置。

解决方法:将上传信息保存在客户端。参见 this posting一个可能的选择。这个想法是,您在服务器端有一个异步方法并在客户端衡量您的进度。

我确信网络上有大量的 javascript 替代品可供上传(我的链接)。

关于c# - ASP.NET MVC4网页异步FTP上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21887085/

相关文章:

c# - 有没有一种方法可以使用通用主机将调试 secret 从asp.net控制台应用程序传递到docker容器?

asp.net - 捕获网页的屏幕截图并获取图像 (ASP.NET)

asp.net - RIA + LINQ to SQL 与 RIA + Entity Framework

asp.net-mvc - 类型或命名空间 ActionExecutingContext 或 ActionFilterAttribute

c# - 当 HashSet<T> 作为对象提供并且 T 可以是任何东西时,如何循环遍历它?

c# - 将克罗地亚语日期字符串解析为 DateTime

php - Asp.net 与 Php 性能对比

c# - 如何从 ASP.NET 读取条码?

javascript - 单击特定行内的按钮时获取行值

c# - 如何在 C# 中将字典的子集提取到另一个字典中?