FTP download azure function
Brax007
Member Posts: 26
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 
And this the function that i'm using to call the ftp download :
[FunctionName("FtpStor")]
public static async Task<IActionResult> 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);
0
Answers
-
Hi, did you find a solution? I am probably going to need Azure function for both FTP upload and download to call from Business Central cloud. Just searching for possibilities right now.0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 322 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions