ftp文件下载工作在localhost但不在服务器上工作

时间:2012-06-20 09:36:21

标签: c# ftp download

string HostName = System.Configuration.ConfigurationSettings.AppSettings["HostName"];
string strUser = System.Configuration.ConfigurationSettings.AppSettings["BasicAuthenticationUser"];
string strPWD = System.Configuration.ConfigurationSettings.AppSettings["BasicAuthenticationPWD"];

strFile = "9788191073072.zip";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HostName + strFile);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(strUser, strPWD);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();

Response.AddHeader("content-disposition", "attachment;filename=" + strFile);

byte[] buffer = new byte[2 * 1024];
int read;

while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0) { Response.OutputStream.Write(buffer, 0, read); }

responseStream.Close();
response.Close();

2 个答案:

答案 0 :(得分:1)

string HostName = System.Configuration.ConfigurationSettings.AppSettings["HostName"];
string strUser = System.Configuration.ConfigurationSettings.AppSettings["BasicAuthenticationUser"];
string strPWD = System.Configuration.ConfigurationSettings.AppSettings["BasicAuthenticationPWD"];

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(HostName + strFile);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(strUser, strPWD);
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = false;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();

Response.ContentType = "application/octet-stream";
Response.AddHeader("content-disposition", "attachment;filename=" + _AudiobookName + ".zip");
int read;
byte[] buffer = new byte[2 * 1024];
while ((read = responseStream.Read(buffer, 0, buffer.Length)) > 0)
{
    Response.OutputStream.Write(buffer, 0, read);
    Response.Flush();
}
responseStream.Close();
response.Close();
Response.Flush();
Response.End();

答案 1 :(得分:0)

如果没有任何真实的细节,这可能需要在生产服务器上进行Mime类型设置。

这也可能是由于您和服务器之间可能存在透明的代理过滤策略。

同样,你真的应该通过Fiddler做一个细粒度的跟踪,看看服务器真正返回了什么,以及请求是否成功完成。

假设这是一个IIS服务器,查看服务器事件日志还可以告诉您应用程序池是否崩溃并重新启动,这似乎没有任何错误但响应不正确。