繁体   English   中英

如何使用C#将.xls文件传输到其他FTP路径

[英]How to Transfer the .xls file to other FTP Path using C#

我是C#的新手,我需要将.xlx文件从指定位置传输到FTP路径(\\ ServerHostName \\ ExtractedFile)。 使用C#代码,请您帮我一下

FtpWebRequest的MSDN页面包含一些在C#和.NET中处理FTP的示例。 示例之一就是您要执行的上传文件的操作。 这个例子是异步的。

https://msdn.microsoft.com/zh-CN/library/system.net.ftpwebrequest(v=vs.110).aspx

在另一页上,有一个更简单的示例:

https://msdn.microsoft.com/zh-CN/library/ms229715(v=vs.110).aspx

using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.UploadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            // Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader("testfile.txt");
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

            response.Close();
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM