上传到FTP文件夹中? [英] Uploading into folder in FTP?

查看:109
本文介绍了上传到FTP文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码来学习如何使用FTP加载文件。如何设置文件上传到的路径或文件夹?


使用System.IO;
使用System.Net;
使用System.Text;

namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main()
{
//获取用于与服务器通信的对象。
FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(ftp://www.contoso.com/test.htm);
request.Method = WebRequestMethods.Ftp.UploadFile;

//此示例假定FTP站点使用匿名登录。
request.Credentials = new NetworkCredential(anonymous,janeDoe@contoso.com);

//将文件的内容复制到请求流中。
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();
}
}
}
}


解决方案

该文件夹是您在创建请求时设置的URL的一部分:ftp://www.contoso的.com / TEST.HTM。如果您使用ftp://www.contoso.com/wibble/test.htm,则文件将被上传到名为 wibble的文件夹



您可能需要先使用 Method = WebRequestMethods.Ftp.MakeDirectory wibble 文件夹。


I am using the following code to learn how to load files with FTP. How do I set the path or folder into which my file will be uploaded ?

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();
            }
        }
    }
}

解决方案

The folder is part of the URL you set when you create request: "ftp://www.contoso.com/test.htm". If you use "ftp://www.contoso.com/wibble/test.htm" then the file will be uploaded to a folder named wibble.

You may need to first use a request with Method = WebRequestMethods.Ftp.MakeDirectory to make the wibble folder if it doesn't already exist.

这篇关于上传到FTP文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆