上传到FTP文件夹中? [英] Uploading into folder in 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 $ c $如果它尚不存在,则使
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屋!