Share via

Data sharing between the server and client app

Peter Liang 3,286 Reputation points
2026-03-17T09:52:43.1233333+00:00

Hi,

Better to share text file between the server and the client app? Any sample to receive a text file on the client app?

Developer technologies | ASP.NET | ASP.NET API

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 16,115 Reputation points Microsoft External Staff Moderator
    2026-03-18T02:27:05.4666667+00:00

    Hi @Peter Liang ,

    Thanks for reaching out.

    For sharing data between your server and client app, it depends on what you want to achieve.

    • Structured data (like a list of items): The easiest and most common way is to send it as JSON through your API. The client can then read it directly and use it in the app, no files needed.
    • Text files (logs, reports, or exports): If you specifically want the client to download or store the data as a file, your server can send it as a text file, and the client can fetch it and save or display it. For example: Server side (ASP.NET Core):
        app.MapGet("/download", async context =>
        {
            var content = "Hello from server!\nThis is a sample text file.";
            context.Response.ContentType = "text/plain";
            await context.Response.WriteAsync(content);
        });
      
      .NET client example:
        using var client = new HttpClient();
        var text = await client.GetStringAsync("https://yourserver.com/download");
        Console.WriteLine(text);
      
      Browser (JavaScript) download example:
        fetch("https://yourserver.com/download")
          .then(response => response.text())
          .then(data => {
              const blob = new Blob([data], { type: 'text/plain' });
              const link = document.createElement('a');
              link.href = URL.createObjectURL(blob);
              link.download = "sample.txt";
              link.click();
          });
      

    Note: Treat these code snippets as examples only - adjust them to fit your project structure and requirements.

    Hope this helps! If my answer was helpful, I would greatly appreciate it if you could follow the instructions here so others with the same problem can benefit as well.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 83,666 Reputation points
    2026-03-18T19:10:29+00:00

    its pretty simple:

    server min api:

    app.MapGet("download/{fileName}", async (string fileName) => 
            {
                var mimeType = "text/plain";
                var path = Path.Combine(app.Environment.ContentRootPath, "Files", $"{fileName}.txt");
            
                if (!File.Exists(path)) return Results.NotFound();
                return Results.File(path, mimeType, $"{fileName}.txt");
            })
                .Produces(StatusCodes.Status200OK)
                .Produces(StatusCodes.Status404NotFound); 
    

    client app:

    using System.Net;
    
    var client = new WebClient();
    var path = "http://localhost:5106/api/download/foo";
    var content = client.DownloadString(path);
    Console.WriteLine(content);
    
    0 comments No comments

  2. Q&A Assist
    2026-03-17T09:53:00.69+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    For an ASP.NET-based server and a client app, a simple and robust way to share a text file is over HTTP(S) using the .NET networking classes. The server exposes the file at a URI, and the client downloads it using WebClient or WebRequest.

    Below is a minimal example pattern based on the provided ASP.NET sample.

    Server side (ASP.NET)

    Expose the text file via an HTTP endpoint or static file:

    • Place the text file in a folder that IIS/ASP.NET can serve (for example, /files/mydata.txt).
    • Or create an ASP.NET handler/controller action that returns the file content.

    The sample in the context uses URIs and the HttpRemoteDownload class to download files from a remote server:

    public class HttpRemoteDownload : RemoteDownload
    {
        public HttpRemoteDownload(string urlString, string descFilePath)
            : base(urlString, descFilePath)
        {
        }
    
        public override bool DownloadFile()
        {
            string fileName = System.IO.Path.GetFileName(this.UrlString);
            string descFilePathAndName = System.IO.Path.Combine(this.DescFilePath, fileName);
            try
            {
                WebRequest myre = WebRequest.Create(this.UrlString);
            }
            catch
            {
                return false;
            }
    
            try
            {
                byte[] fileData;
                using (WebClient client = new WebClient())
                {
                    fileData = client.DownloadData(this.UrlString);
                }
    
                using (FileStream fs = new FileStream(descFilePathAndName, FileMode.OpenOrCreate))
                {
                    fs.Write(fileData, 0, fileData.Length);
                }
    
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("download field", ex.InnerException);
            }
        }
    }
    

    Client side: receiving the text file

    On the client, point to the server URI of the text file and save it locally. For example:

    string fileUrl = "https://server.example.com/files/mydata.txt"; // server URI
    string localFolder = @"C:\ClientData";                         // local folder
    
    var downloader = new HttpRemoteDownload(fileUrl, localFolder);
    bool ok = downloader.DownloadFile();
    
    if (ok)
    {
        // Now read the text content if needed
        string localPath = Path.Combine(localFolder, Path.GetFileName(fileUrl));
        string text = File.ReadAllText(localPath);
        // use text
    }
    

    This approach:

    • Uses HTTP(S) and URIs to identify the file on the server.
    • Uses WebClient.DownloadData to retrieve the file bytes.
    • Writes the bytes to a local file via FileStream, then reads it as text.

    If FTP is preferred, the same pattern can be followed using FtpRemoteDownload and FtpWebRequest from the sample.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.