Martyn Ball
Martyn Ball

Reputation: 4895

C# Upload file with no extension

I have got the following code, I need to upload multiple files which don't have an extension/type assigned to them. The following code below works, but not on these files as it's trying to make a file for example called: ftp.server/file

Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            String sourcefilepath = @"path"; // e.g. “d:/test.docx”
            String ftpurl = @"ftp"; // e.g. ftp://serverip/foldername/foldername
            String ftpusername = @"user"; // e.g. username
            String ftppassword = @"pass"; // e.g. password

            string[] filePaths = Directory.GetFiles(sourcefilepath);

            // Get the object used to communicate with the server.
            foreach (string filename in filePaths)
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpurl + Path.GetFileName(filename));
                request.Method = WebRequestMethods.Ftp.UploadFile;

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

                // Copy the contents of the file to the request stream.
                StreamReader sourceStream = new StreamReader(sourcefilepath);
                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();

                Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

                response.Close();
            }
        }
    }
}

The files are from an iPhone backup, iPhone backups don't give the files extensions so I need a work around.

Upvotes: 2

Views: 391

Answers (1)

Martyn Ball
Martyn Ball

Reputation: 4895

The solution I found was to copy the files and add a dummy extension.

Upvotes: 1

Related Questions