[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); } }
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);
Answers