0

So i have a file locally that needs to keep the same name. But when i upload the file to the FTP i want to rename it, or add some ekstra to the file name. here is how i upload the file.

string localPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            string fileName = "//XmlDocument.xml";    
FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("ftp://000.000.000/Documents" + fileName);
            requestFTPUploader.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
            requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;

            FileInfo fileInfo = new FileInfo(localPath + fileName);
            FileStream fileStream = fileInfo.OpenRead();

            int bufferLength = 2048;
            byte[] buffer = new byte[bufferLength];

            Stream uploadStream = requestFTPUploader.GetRequestStream();
            int contentLength = fileStream.Read(buffer, 0, bufferLength);

            while (contentLength != 0)
            {
                uploadStream.Write(buffer, 0, contentLength);
                contentLength = fileStream.Read(buffer, 0, bufferLength);
            }

            uploadStream.Close();
            fileStream.Close();

            requestFTPUploader = null;
Code_Viking
  • 618
  • 1
  • 8
  • 26
  • Why don't you simply rename it then? System.IO.File.Move("oldfilename", "newfilename"); (or just copy it) – L-Four Dec 14 '15 at 11:08
  • So, you have a file "file1" and, under certain conditions, you want to rename it to "file2" but by also keeping the original "file1". I don't know, perhaps there is a hidden complexity which cannot see, but what about copying the file by setting the new name to the copy, uploading this new version and deleting it afterwards? Basically, what you would be doing in this same situation yourself in case of having to perform this action manually. – varocarbas Dec 14 '15 at 11:09
  • @L-Three i am new to working with ftps. But i have tried to add somting to the 'filename' property but then it gives me an error. It says that the file cannot be found? – Code_Viking Dec 14 '15 at 11:13
  • @L-Three i am not familiar with System.IO.File.Move. What i am trying to is to read the local file i have then rename it and after that upload it to the ftp. All while nothing happens to the local fil. The local file is read only – Code_Viking Dec 14 '15 at 11:18
  • You are not new with FTP, you are new with C# file management. FTP actions (or any other action dealing with files), doesn't take care itself of everything. For example: all what refers to files is being managed by System.IO and this is what you have to understand (eventually the given method might include some reference to System.IO actions). In summary: you are not understanding how to deal with files; learn that and then apply such a knowledge to any other action dealing with files. – varocarbas Dec 14 '15 at 11:19
  • @varocarbas yes sorry. I am new with C# file management – Code_Viking Dec 14 '15 at 11:20
  • 1
    There is nothing bad with it. Just tried to highlight the point for the future (such that you will not have the same problem when dealing with other issues related to files). – varocarbas Dec 14 '15 at 11:20

1 Answers1

1

There is no relation whatsoever between the name of the file you're trying to upload and the name on the server you're uploading it to.

You can upload foo.jpg as bar.exe and no one will bat an eye.

So change this:

FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("..." + fileName);

To this:

FtpWebRequest requestFTPUploader = (FtpWebRequest)WebRequest.Create("..." + ftpFileName);

And you're good to go.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • if i try this i get a new error. at this line of code `Stream uploadStream = requestFTPUploader.GetRequestStream();` it says that, remote server returned an error. 550 file is not available. – Code_Viking Dec 14 '15 at 11:28
  • Then you're not setting `Method = WebRequestMethods.Ftp.UploadFile`. – CodeCaster Dec 14 '15 at 11:29
  • i call this in the code `requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;`. is that what you mean? – Code_Viking Dec 14 '15 at 11:31
  • If you try searching, you'll find http://stackoverflow.com/questions/17471745/ftp-getresponse-error-550-file-unavailable – CodeCaster Dec 14 '15 at 11:37