0
我想了解的FtpWebRequest多specificaly MSDN文檔,how to upload using FTP and C#的FtpWebRequest - 問題與MSDN例如
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","[email protected]");
// 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();
}
}
}
}
在上面的代碼,他們引用2種文件類型。我是否認爲testfile.txt是本地計算機上的「源」文件,test.htm是否也會重命名爲testfile.txt?
嗨喬恩。是的,你在過去向我解釋過「使用」(就像你的書一樣)!因此,使用上面的代碼,我可以將目的地更改爲:www.domain.com/myFile.zip?這個例子令人困惑,因爲它是一個FTP(一個web uri),並將文件保存爲與web相關的文件類型! – Dave 2013-02-11 13:25:14
@DaveRook:是的,你可以...雖然這很奇怪,因爲我真的不希望文本文件也是一個有效的zip文件(而它可能是一個合理的HTML文件)。通常我會希望你保持相同的文件擴展名,儘管... – 2013-02-11 13:28:20
是的,這很好,我的源已經是.gz文件,所以目的地!完美,再次感謝您花時間。 – Dave 2013-02-11 13:29:04