【问题标题】:How to overwrite file via FTP如何通过 FTP 覆盖文件
【发布时间】:2014-04-07 07:42:02
【问题描述】:

我的程序生成我上传到 FTP 的 txt 文件。在文件名中,我有一个时间跨度(没有秒数)。

所以当我在一分钟内生成两次文件时,我的文件名相同。如果 ftp 上存在此类文件,我无法发送新文件,引发异常。

但我只需要默默地覆盖它。如何做到这一点?

目前我使用这样的功能

public bool FtpUploadFile(string filePath, FTPParameters ftpParams)
{
    // Get the object used to communicate with the server.
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpParams.Server + "/" + ftpParams.Folder + "/" + filePath);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.UsePassive = false;

    // This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential(ftpParams.User, ftpParams.Password);

    // Copy the contents of the file to the request stream.
    StreamReader sourceStream = new StreamReader(Path.Combine(Context.Current.SystemSettings.StorePath, filePath));
    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();

    return response.ExitMessage.Trim() == string.Empty;
}

【问题讨论】:

标签: c# ftp


【解决方案1】:

你不能。您必须先删除现有文件。我建议你重命名服务器上已经存在的文件,然后上传新文件,只有在成功的情况下你才删除旧文件以确保至少还剩下一个文件。

【讨论】:

  • 这是最好的方法。
  • 这仍然是一个有效的答案吗?
【解决方案2】:

我试了几次,它确实覆盖了目标 这是我的功能

    public bool Upload(FileInfo fi, string targetFilename)
    {
        //copy the file specified to target file: target file can be full path or just filename (uses current dir)

        //1. check target
        string target;
        if (targetFilename.Trim() == "")
        {
            //Blank target: use source filename & current dir
            target = this.CurrentDirectory + fi.Name;
        }
        else if (targetFilename.Contains("/"))
        {
            //If contains / treat as a full path
            target = AdjustDir(targetFilename);
        }
        else
        {
            //otherwise treat as filename only, use current directory
            target = CurrentDirectory + targetFilename;
        }
          string URI = Hostname + target;

        //perform copy
        System.Net.FtpWebRequest ftp = GetRequest(URI);

        //Set request to upload a file in binary
        ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
        ftp.UseBinary = true;

        //Notify FTP of the expected size
        ftp.ContentLength = fi.Length;
    //    ftp.O

        //create byte array to store: ensure at least 1 byte!
        const int BufferSize = 2048;
        byte[] content = new byte[BufferSize - 1 + 1];
        int dataRead;

        //open file for reading
        using (FileStream fs = fi.OpenRead())
        {
            try
            {
                //open request to send
                using (Stream rs = ftp.GetRequestStream())
                {
                    do
                    {
                        dataRead = fs.Read(content, 0, BufferSize);
                        rs.Write(content, 0, dataRead);
                    } while (!(dataRead < BufferSize));
                    rs.Close();
                }

            }
            catch (Exception)
            {

            }
            finally
            {
                //ensure file closed
                fs.Close();
            }

        }


        ftp = null;
        return true;

    }

【讨论】:

    猜你喜欢
    • 2015-01-25
    • 2012-07-10
    • 2013-11-16
    • 2016-11-23
    • 1970-01-01
    • 2021-06-26
    • 2022-01-12
    • 2017-04-15
    • 1970-01-01
    相关资源
    最近更新 更多