FtpWebRequest use STOU mode can not upload

Hill 0 Reputation points
2023-02-24T02:11:52.6033333+00:00

I use FtpWebRequest to upload a file to FTP, if I set the method to WebRequestMethods.Ftp.UploadFileWithUniqueName, following exception will be catched in line

Stream requestStream = request.GetRequestStream();

System.Net.WebException: 'The remote server returned an error: (550) File unavailable (e.g., file not found, no access).'

This is my source code. And the .NET Framework is .NET Framework 4

FtpWebRequest request;

                string absoluteFileName = Path.GetFileName(fileName);

                request = WebRequest.Create(new Uri(string.Format(@"ftp://{0}/{1}", fullAddr, absoluteFileName))) as FtpWebRequest;
                request.Method = WebRequestMethods.Ftp.UploadFileWithUniqueName;
                request.UseBinary = true;
                request.KeepAlive = false;
                request.UsePassive = false;
                request.Credentials = new NetworkCredential(user, pass);
                request.Proxy = null;                

                using (FileStream fs = File.OpenRead(fileName))
                {
                    request.ContentLength = fs.Length;
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    fs.Close();
                    fs.Dispose();
                    Stream requestStream = request.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
                    requestStream.Flush();
                    requestStream.Close();
                }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,345 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,201 Reputation points Microsoft External Staff
    2023-03-06T09:08:33.7433333+00:00

    Hi, welcome to Microsoft Q&A.

    Check if the policy for the target FTP service is set to read-only?

    When the destination file already exists, STOU creates a new file with a unique file name to store the uploaded file.

    Best Regards. 

    Jiachen Li 

    ---------- 

    If the answer is helpful, please click "Accept Answer" and upvote it. 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.   

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.