简体   繁体   English

如何使用ftp服务器上传文件?

[英]How I can upload file with ftp server?

I have a host for my web site and this host have not enough space for my file and I get another host to save my file in this host I have ftp adress and username and pass 我有一个主机用于我的网站,这个主机没有足够的空间存放我的文件,我得到另一个主机将我的文件保存在这个主机中我有ftp地址和用户名并通过

I find this code 我找到了这段代码

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

but

how i can upload my file to another host with ftp and get url place of save file? 我如何使用ftp将我的文件上传到另一台主机并获取保存文件的url位置?

you choose the path where you upload to ftp appending directories to the FTP address: 您选择上传到ftp附加目录到FTP地址的路径:

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

string FileName = "yourfile.txt";

And here you have a working example I found once: 在这里你有一个我发现过的工作例子:

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

Extracted from here: 从这里提取:

Upload file on ftp 在ftp上传文件

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

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