gpt4 book ai didi

c# - 我如何使用ftp服务器上传文件?

转载 作者:行者123 更新时间:2023-11-30 17:04:26 25 4
gpt4 key购买 nike

我的网站有一个主机,这个主机没有足够的空间来存放我的文件,我让另一个主机把我的文件保存在这个主机上我有 ftp 地址和用户名并通过

我找到这段代码

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");

但是

我如何使用 ftp 将我的文件上传到另一个主机并获取保存文件的 url 位置?

最佳答案

你选择你上传到ftp的路径附加目录到FTP地址:

string CompleteDPath = "ftp://yourFtpHost/folder1/folder2/";

string FileName = "yourfile.txt";

这里有一个我曾经找到的工作示例:

WebRequest reqObj = WebRequest.Create(CompleteDPath + FileName);
The following script work great with me for uploading files and videos to another servier via ftp.

FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(ftpurl + "" + username + "_" + filename);
ftpClient.Credentials = new System.Net.NetworkCredential(ftpusername, ftppassword);
ftpClient.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
ftpClient.UseBinary = true;
ftpClient.KeepAlive = true;
System.IO.FileInfo fi = new System.IO.FileInfo(fileurl);
ftpClient.ContentLength = fi.Length;
byte[] buffer = new byte[4097];
int bytes = 0;
int total_bytes = (int)fi.Length;
System.IO.FileStream fs = fi.OpenRead();
System.IO.Stream rs = ftpClient.GetRequestStream();
while (total_bytes > 0)
{
bytes = fs.Read(buffer, 0, buffer.Length);
rs.Write(buffer, 0, bytes);
total_bytes = total_bytes - bytes;
}
//fs.Flush();
fs.Close();
rs.Close();
FtpWebResponse uploadResponse = (FtpWebResponse)ftpClient.GetResponse();
value = uploadResponse.StatusDescription;
uploadResponse.Close();

摘自这里:

Upload file on ftp

关于c# - 我如何使用ftp服务器上传文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17463859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com