Question FTP download/Rename function async C#

bnasr92

Member
Joined
Oct 29, 2020
Messages
5
Programming Experience
Beginner
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 the erp I am using. 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 :

C#:
    [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);

        }



    

    

    }
 
Where exactly do you want to download to? To your machine? Or to the Azure instance?
 
See:

Instead of writing the response stream out to the console like in the sample code, use Stream.CopyTo() to copy the response stream into FileStream returned to you by File.OpenWrite(). There is no need to rename. Just pass in your desired filename in as the parameter to File.OpenWrite().
 
See:

Instead of writing the response stream out to the console like in the sample code, use Stream.CopyTo() to copy the response stream into FileStream returned to you by File.OpenWrite(). There is no need to rename. Just pass in your desired filename in as the parameter to File.OpenWrite().
Sorry I was misunderstood, I want the return to be a stream to pass it in another function later
 
Solution
Make up your mind. Do you need to download the data to your machine? Or do you need to pipe the output of one Azure function to another function?
 
Make up your mind. Do you need to download the data to your machine? Or do you need to pipe the output of one Azure function to another function?
i want to pipe the output to a stream juste like the input of the upload function that i've posted
 
Azure functions have more efficient ways of transporting data around that FTP. See the link in post #7 for your options for moving data around. I don't use Azure functions but from the samples I've seen, the blob storage seems to be one of the go-to data storage mediums.
 
Back
Top Bottom