2

I've been spending all day trying to figure this one out, but with no luck.

Basically I have a bunch of files and folders, that I want to upload to a FTP server, and everything works great on localhost. But when I deploy it to azure, it stops working. In my head it makes no sense, and my initial thought was it had to do with a firewall, upload timeout or something else.

Do you see anything I am missing in my code, or do I need to do some configuration on azure or my ftp server ?

Edit: I forgot to include the error message:

"The underlying connection was closed: An unexpected error occurred on a receive Problem Id:System.Net.WebException at simplecms.Models.FtpConn.UploadFile"

public class FtpConn
{

    public string UploadCms(string address, string username, string password, string host, bool userftp, string guid)
    {
        CreateFileUID(guid);

        string location;
        if (userftp) 
        {
            location = address + "/simplecms/";
            host = host + "/simplecms/";
        }
        else 
        {
            location = address + "/simplecms" + guid + "/";
            host = host + "/simplecms" + guid + "/";
        }

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(location);
        request.Credentials = new NetworkCredential(username, password);
        //request.Timeout = -1;
        request.UsePassive = true;
        request.UseBinary = true;
        request.Timeout = 1000000000;
        request.KeepAlive = false;
        request.ReadWriteTimeout = 1000000000;
        request.Method = WebRequestMethods.Ftp.MakeDirectory;

        using (var resp = (FtpWebResponse)request.GetResponse())
        {
            Console.WriteLine(resp.StatusCode);

        }

        string cmsFolder = "wwwroot/dist/";
        string[] cmsFiles = Directory.GetFiles(cmsFolder);
        foreach (string file in cmsFiles)
        {
            string path = Path.GetFullPath(file);
            string name = Path.GetFileName(file);
            UploadFile(username, password, location, path, name);
        }

        string[] staticFiles = Directory.GetDirectories(cmsFolder);
        string[] subs = Directory.GetDirectories(cmsFolder + "static/");
        foreach (string file in staticFiles)
        {
            CreateDirectory(username, password, location + "/" + Path.GetFileName(file));
        }

        foreach (string folder in subs)
        {
            CreateDirectory(username, password, location + "/static/" + Path.GetFileName(folder));

            foreach (string subfile in Directory.GetFiles(folder))
            {
                string path = Path.GetFullPath(subfile);
                string name = Path.GetFileName(subfile);
                UploadFile(username, password, location + "/static/" + Path.GetFileName(folder) + "/" + Path.GetFileName(subfile), path, "");
            }

        }

        return host;
    }

    private void UploadFile(string username, string password, string location, string filePath, string fileName)
    {


        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(location + fileName);
        request.Credentials = new NetworkCredential(username, password);

        //request.Timeout = -1;
        request.UsePassive = true;
        request.UseBinary = true;
        request.Timeout = 1000000000;
        request.KeepAlive = false;
        request.ReadWriteTimeout = 1000000000;
        request.Method = WebRequestMethods.Ftp.UploadFile;

        FileStream stream = File.OpenRead(filePath);
        byte[] buffer = new byte[stream.Length];
        stream.Read(buffer, 0, buffer.Length);
        stream.Close();

        Stream reqStream = request.GetRequestStream();
        reqStream.Write(buffer, 0, buffer.Length);
        reqStream.Close();
    }

    private void CreateDirectory(string username, string password, string newDirectory)
    {
        try
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(newDirectory);
            ftpRequest.Credentials = new NetworkCredential(username, password);
            //ftpRequest.Timeout = -1;
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = false;
            ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Console.WriteLine(ex); }
        return;
    }

    private void CreateFileUID(string uid)
    {
        string folderName = "wwwroot/dist/";
        string[] txtList = Directory.GetFiles(folderName, "*.txt");
        foreach (string f in txtList)
        {
            File.Delete(f);
        }

        string fileName = "uid.txt";
        string pathString = Path.Combine(folderName, fileName);
        using (FileStream fs = File.Create(pathString))
        {
            byte[] SimpleCmsUID = new UTF8Encoding(true).GetBytes(uid);
            fs.Write(SimpleCmsUID, 0, SimpleCmsUID.Length);
        }
    }

}
  • You might have a better idea if you could log exceptions somewhere – Crowcoder Dec 18 '17 at 20:30
  • Thanks! I totally forgot to include that in my question. On azure I actually get the error messages. I will edit my question. – Andreas Baggesgaard Dec 18 '17 at 20:35
  • What "azure" is that? Is it full VM? Or just a "website"? + Can post [`FtpWebRequest` log file](https://stackoverflow.com/q/9664650/850848)? – Martin Prikryl Dec 18 '17 at 20:55
  • knowing the actual web exception might be helpful to diagnose your issue ... can you get that ? – Muqeet Khan Dec 19 '17 at 00:27
  • Perhaps include a stack trace as well? – Rob Reagan Dec 19 '17 at 02:06
  • If you are using Azure webapp, you could get [remote to debug](https://learn.microsoft.com/en-us/Azure/app-service/web-sites-dotnet-troubleshoot-visual-studio#remotedebug) it to get detail error. And also please make sure the folder and file path are correctly on the azure. – Tom Sun - MSFT Dec 19 '17 at 05:09

0 Answers0