Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ftpWebRequest下载Word、Pdf或Excel文档?_C#_Ftpwebrequest - Fatal编程技术网

C# 如何使用ftpWebRequest下载Word、Pdf或Excel文档?

C# 如何使用ftpWebRequest下载Word、Pdf或Excel文档?,c#,ftpwebrequest,C#,Ftpwebrequest,我想从ftp位置下载文档?常见的是Word或Pdf文档。你怎么知道的 internal int Download(string filename, string sourcePath, string localPath, out string message) { message = string.Empty; int download = 0; try { string fullFilen

我想从ftp位置下载文档?常见的是Word或Pdf文档。你怎么知道的

    internal int Download(string filename, string sourcePath, string localPath, out string message)
    {
        message = string.Empty;
        int download = 0;

        try
        {
            string fullFilename = "ftp://" + host + "/" + sourcePath + "/" + fiilename;
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(fullFilename);
            ftpRequest.Credentials = new NetworkCredential(username, password);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = false;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            if (!FileExists(fullFilename))
            {
                message = string.Format("Bestand {0} niet gevonden.", fullFilename);
                download = 1;
            }

            string tempFilename = Path.Combine(localPath, fiilename);
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            Stream responseStream = ftpResponse.GetResponseStream();
            FileStream fileStream = new FileStream(tempFilename, fileMode.Create);
            int length = 2048;
            Byte[] buffer = new Byte[length];

            int bytesRead = responseStream.Read(buffer, 0, length);

            while (bytesRead > 0)
            {
                fileStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, length);
            }

            fileStream.Close();
            responseStream.Close();            }
        catch (WebException ex)
        {
            //Console.WriteLine("Upload File Complete, status {0}", ftpResponse.StatusDescription);
            message = ((FtpWebResponse)ex.Response).StatusDescription;
            download = 2;
        }
        catch (Exception ex)
        {
            message = ex.Message;
            download = 3;
        }
        finally
        {
            /* Resource Cleanup */
            if (ftpResponse != null) ftpResponse.Close();
            if (ftpRequest != null) ftpRequest = null;
        }

        return download;
    }

这是一个有效的来源

internal int Download(string filename, string sourcePath, string localPath, out string message)
{
    message = string.Empty;
    int download = 0;

    try
    {
        string fullFilename = "ftp://" + host + "/" + sourcePath + "/" + filename;
        if (!FileExists(fullFilename))
        {
            message = string.Format("Bestand {0} niet gevonden.", fullFilename);
            download = 1;
        }
        else
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(fullFilename);
            ftpRequest.Credentials = new NetworkCredential(username, password);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = false;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

            string tempFilename = Path.Combine(localPath, filename);
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            Stream responseStream = ftpResponse.GetResponseStream();
            FileStream fileStream = new FileStream(tempFilename, fileMode.Create);
            int length = 2048;
            Byte[] buffer = new Byte[length];

            int bytesRead = responseStream.Read(buffer, 0, length);

            while (bytesRead > 0)
            {
                fileStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, length);
            }

            fileStream.Close();
            responseStream.Close();            
        }
    }   
    catch (WebException ex)
    {
        //Console.WriteLine("Upload File Complete, status {0}", ftpResponse.StatusDescription);
        message = ((FtpWebResponse)ex.Response).StatusDescription;
        download = 2;
    }
    catch (Exception ex)
    {
        message = ex.Message;
        download = 3;
    }
    finally
    {
        /* Resource Cleanup */
        if (ftpResponse != null) ftpResponse.Close();
        if (ftpRequest != null) ftpRequest = null;
    }

    return download;
}

抱歉,FileExists中存在另一个webrequest。我认为KeepAlive=false;当它暂时离开功能时,不会保留内容。