【问题标题】:How I can upload file with ftp server?如何使用 ftp 服务器上传文件?
【发布时间】:2013-07-04 07:05:31
【问题描述】:

我的网站有一个主机,但这个主机没有足够的空间存放我的文件,我让另一个主机将我的文件保存在这个主机中我有 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 位置?

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

    您选择上传到 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

    【讨论】:

    • 什么是 ftpurl 以及用户名和文件名
    • 这是一个例子。在 FtpWebRequest 中,您只需发送 URL 字符串。用户名和密码用于 Credentials 行;)
    • 我在这里说 FtpWebRequest.Create(ftpurl + "" + username + "_" + filename);
    • 我知道你的意思。只需将 "ftpurl + "" + username + "_" + filename" 替换为您的真实 URL,例如:FtpWebRequest.Create("localhost/myftpfolder" + filename);
    • 我上传了我的文件,然后我在 HttpPostedFileBase 上传文件中得到了它,现在我的文件在上传文件中我如何使用你对上传文件的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-25
    • 2014-10-17
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多