我正在尝试使用Jquery Ajax请求从FTP服务器下载PDF文件。我提到http://www.dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5/。
我的Jquery ajax调用如下
$.ajax({
xhr: function () {
var xhr = new window.XMLHttpRequest();
//Download progress
xhr.addEventListener("progress", function (evt) {
console.log("Event :"+evt.lengthComputable);
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: "Downloader.ashx",
success: function (data) {
//Do something success-ish
}
});
下载文件的My C#通用处理程序代码如下
public void ProcessRequest(HttpContext context)
{
DownLoadFilesFromFTp("MyFile.pdf", "Foldername");
}
public bool DownLoadFilesFromFTp(string fileName,string ftpFolder)
{
//Create FTP Request.
try
{
string Ftp_Host = System.Configuration.ConfigurationManager.AppSettings["Ftp_Host"];
string Ftp_UserName = System.Configuration.ConfigurationManager.AppSettings["Ftp_UserName"];
string Password = System.Configuration.ConfigurationManager.AppSettings["Password"];
string downloadpath= System.Configuration.ConfigurationManager.AppSettings["downloadpath"];
//Fetch the Response and read it into a MemoryStream object.
string ftpurl = Ftp_Host + ftpFolder + "/" + fileName;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpurl));
reqFTP.Credentials = new NetworkCredential(Ftp_UserName, Password);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = null;
//if (fileName.Substring(fileName.Length - 3, 3) == "pdf" || fileName.Substring(fileName.Length - 3, 3) == "PDF")
//{
writeStream = new FileStream(downloadpath + fileName, FileMode.Create);
//}
int Length = 2048; // 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
responseStream.Close();
writeStream.Close();
response.Close();
return true;
}
catch (WebException wEx)
{
return false;
}
catch (Exception ex)
{
return false;
}
}
当我运行代码文件时,下载到文件夹没有任何问题,并且在Ajax上调用
if(evt.lengthComputable){
}
当我控制退出时,我得到了以下结果
始终返回false,因此我无法跟踪进度。
1)代码有什么问题吗?
2)在下载pdf时显示进度条的任何替代方法
答案 0 :(得分:3)
对于上传的字节,显示进度条非常容易。只需监控xhr.upload.onprogress
事件即可。浏览器知道它必须上传的文件的大小以及上传数据的大小,因此它可以提供进度信息。
对于下载的字节,这有点困难,因为在这种情况下浏览器唯一知道的是它接收的字节大小。
evt.lengthComputable
0
的原因是浏览器没有 知道服务器请求中将发送多少字节。
有一个解决方案,只需在服务器上设置内容长度标题,如下所示,以获得浏览器将接收的字节总大小。< / p>
// prepare the response to the client. resp is the client Response
var resp = HttpContext.Current.Response;
// Add Content-Length of the file to headers
// if the headers is not set then the evt.loaded will be 0
resp.AddHeader("Content-Length", "lengthOfYourFile");
答案 1 :(得分:2)
你的代码JS方面看起来很好。 我不是C#程序员,但我观察到C#服务器端,下载文件ftp并将其保存到磁盘服务器,但从未响应/发送PDF二进制文件到JS SIDE? 从JS方面下载0字节。和evt.lengthComputable总是假/ 0。