2014-02-19 90 views
2

我的问题是完全一样的是 Monker已经张贴在这里:异步FTP上传

总结问题,在网页上:

  • 用户点击上传文件
  • 用户选择一个大文件上传
  • 用户点击“上传”按钮
  • 应用会说“你文件正在上传。你将它完成时”
  • 用户继续使用该应用程序正常,也许另一个窗口或灯箱出现显示一个进度条,为什么我又在这里张贴通知。

  • 我的名声并没有让我做评论或回答这个职务。
  • 我想告诉你我的全部的代码试图
  • 的职位可是没有一个很好的答案却把事情。

一些用户建议我们使用像Telerik这样的第三方组件,但使用演示可以看到它在文件上传时冻结页面。我用很多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" /> 
} 

在控制器:

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> 

我必须向您报告此应用程序在本地托管成功,这意味着在选择文件并按下UPLOAD按钮后,我们可以看到立即TesteDownload.cshtml页面和柜台工作的百分比上传成长。当我在不在我的计算机上的服务器上发布此应用程序时,只有当文件传输完成后,页面才会冻结,而其他页面才会显示。

我想知道真正发生了什么,我的意思是。异步方法真的可以在服务器上使用ajax吗?本地主机和服务器之间是否存在不允许此功能的区别?

从现在开始我很感激任何帮助。

[S 莱奥

回答

0

我觉得你在这里混了一些想法。所以要回答你的直接问题:是的,$.get是真正的ajax,你可以使用浏览器扩展,比如Firefox中的Firebug或IE中的DevTools。但是:你不这样做,AJAX应该完成。你有2个循环:1个客户端和另一个在服务器上。

您的服务器环路并没有真正连接到您的客户端环路,因此我认为结果很简单。例如,如果您在本地使用Visual Studio中的IISExpress(您只需点击F5),则会遇到与网络上的真实IIS计算机不同的行为。

StatusUpload是另一个问题,因为你会以轮询方式调用它,你也必须在这里处理被阻塞的线程。它实际上取决于其他100多项其他功能,例如服务器上的应用程序池配置。

解决方案:将上传信息保留在客户端。有关可能的替代方法,请参见this posting。这个想法是,你在服务器端有一个异步方法,并在客户端测量你的进度。

我敢肯定,在网上有很多javascript-alternatives的uploadify(我的链接)。

+0

好的,我记得有两个不同的循环,一个在客户端,另一个在服务器上,我的尝试是使用客户端上的ajax来获取服务器上载文件的状态。但你是对的,我不确定我是否正确地做了这件事。 我读过你建议的帖子,我尝试使用de uploadfy组件,但是我遇到了其他人也面临的问题,“uploadify.swf?preventwfcaching =”在这个组件的最新版本上。 我需要学习和了解更多的一件事是你说的线程... – LeoFraietta