web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

FTP download azure function

(0) ShareShare
ReportReport
Posted on by 60

I have function for uploading a file to ftp and I woud like to know the equivalent function for downloading and renaming the file. I have no experience with c# I only use this functions to post them to azure function and then consume them in BC. I would be thankful is anyone could show or guide me to have the equivalent function for the download and the one for rename; Below is the upload function :

    [FunctionName("FtpStor")]
    public static async Task Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
    
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        dynamic data = JsonConvert.DeserializeObject(requestBody);
    
        string fileUri = data.fileUri;
        string username = data.username;
        string password = data.password;
        string filename = data.filename;
        string ContentFileToBase64String = data.ContentFileToBase64String;

        var bytes = Convert.FromBase64String(ContentFileToBase64String);
        Stream fileStream = new MemoryStream(bytes);
        
        //Upload to FTP
        FtpWebRequest ftpWebRequest =  (FtpWebRequest)WebRequest.Create(fileUri);
        ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
        ftpWebRequest.Credentials = new NetworkCredential(username,password);
        ftpWebRequest.UsePassive = true;

        using (Stream  requestStream = ftpWebRequest.GetRequestStream())
        {
            await fileStream.CopyToAsync(requestStream);
        }
        fileStream.Dispose();

        using (FtpWebResponse  response = (FtpWebResponse)await ftpWebRequest.GetResponseAsync())
        {
            string result = response.StatusDescription;
            
        return response.StatusCode == FtpStatusCode.ClosingData
            ? (ActionResult)new OkObjectResult(result)
            : new BadRequestObjectResult(result);
        }

    
    
    }


And this the function that i'm using to call the ftp download :

        RequestContent.Clear();
        Client.Clear();
        fileuri := 'ftp://*****:21/TEST/file.txt';
        uri := 'https://*****.azurewebsites.net/api/FtpDownload?';
        RequestBody := '{';
        RequestBody  = 'fileUri:"'   fileUri   '",';
        RequestBody  = 'username:"******",';
        RequestBody  = 'password:"*****",';
        RequestBody  = '}';
        RequestContent.WriteFrom(RequestBody);
        RequestContent.GetHeaders(RequestHeaders);
        RequestHeaders.Clear();
        RequestHeaders.Add('Content-Type', 'application/json');
        RequestMessage.Content := RequestContent;
        Client.Post(uri, RequestContent, ResponseMessage);
        ResponseMessage.Content().ReadAs(OutputString);
        tempblob.CreateInStream(FromInStream);
        FromInStream.Read(OutputString);
        Fromfile := 'file.txt';
        DownloadFromStream(FromInStream, '', '', '', Fromfile);



I have the same question (0)
  • Terrance Morgan Profile Picture
    515 on at
    RE: FTP download azure function

    It's probably not possible with Fluent FTP.

    There's FtpClient.Download method which takes Stream.

    public bool Download(Stream outStream, string remotePath, IProgress<double> progress = null)
    

    But it does not look like there's API to get "blob upload Stream".

    Conversely, you cannot get "FTP download Stream" from FluentFTP (which you would be able to use with blob API).


    But you can use native .NET FtpWebRequest FTP client, which has API to get "FTP download Stream":

    public static async System.Threading.Tasks.Task RunAsync([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log)
    {
        var ftpUserName = "xxxxxx";
        var ftpPassword = "xxxxxxxxx";
        var filename = "test.png";
        string blobConnectionString = "xxxxxxx";
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(blobConnectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference("myblobcontainer");
        FtpWebRequest fileRequest = (FtpWebRequest)WebRequest.Create("">ftp://xxxxxx/" + filename);
        fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;
        fileRequest.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
        FtpWebResponse fileResponse = (FtpWebResponse)fileRequest.GetResponse();
        Stream fileStream = fileResponse.GetResponseStream();
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
    
        await blockBlob.UploadFromStreamAsync(fileStream);
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Nimsara Jayathilaka. Profile Picture

Nimsara Jayathilaka. 3,846

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 3,157

#3
Sumit Singh Profile Picture

Sumit Singh 2,903

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans