Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gRPC c# file download/upload + web client side

Tags:

c#

grpc

grpc-web

The main problem:

I am looking for a solution to improve upload/download speed for large files. I've heard about relatively new technology gRPC. I understand that it is good for server to server communication e.g. microservices architecture.

Client

However I need file upload/download component on front end (browser) ( something like Fine uploader, jQuery file upload plugin, Resumable.js) with gRPC support. I can do support by myself. But I don't know how and what and if it is possible at all. So I need an example or an advise or pointing to the right direction. No matter JS side: vanilla, react, angular ...

Server side

  • c# preferable
  • node.js possible
  • java workable

Research done on the subject

  • gRPC Java File Download Example - An answer was given here as links to sample code. Unfortunately links broken.
  • SERVER STREAMING WITH GRPC AND .NET CORE - A client side is console.
  • Update UploadFileClient.java - as above
  • gRPC for .NET Examples - there is no example I need
  • The state of gRPC in the browser and his repo Test various implementations of gRPC-Web Clients with various implementations of gRPC-Web proxies - very interesting (I think it's possible somehow implement client in browser), however as my knowledge in gRPC is very low it's too complicated for me to accomplish my task

Please help or at least say that it is impossible

like image 884
Albert Lyubarsky Avatar asked Nov 07 '25 03:11

Albert Lyubarsky


1 Answers

I understand this is a bit old. But wanted to post this thing. I did transfer a file using C# grpc. Please see below a basic grpc file reque and response

  1. Protocol file
    syntax = "proto3";
    
    option csharp_namespace = "GrpcGreeter";
    
    package greet;
    
    // The greeting service definition. 
    
    service Greeter {
      // Sends a greeting

      rpc SayHello (HelloRequest) returns (HelloReply);
    }
    
    // The request message containing the user's name.

    message HelloRequest {
      string FileName = 1;
    }
    
    // The response message containing the greetings.

    message HelloReply {
      string FileName = 1;
      bytes data=2; 
    }

  1. gRPC service file
    using Google.Protobuf;
    
    using Grpc.Core;
    

    using GrpcGreeter;

    using static System.Net.Mime.MediaTypeNames;
    
    namespace GrpcGreeter.Services
    {
        

    public class GreeterService : Greeter.GreeterBase
        

    {

            private readonly ILogger<GreeterService> _logger;


            public GreeterService(ILogger<GreeterService> logger)
            {
                _logger = logger;
            }
    
            public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
            {
                return Task.FromResult(new HelloReply
                {
                    FileName =  request.FileName,
                    Data = ByteString.CopyFromUtf8(File.ReadAllText(@"D:\files\"+ request.FileName))
                });
            }
        }
    }
  1. gRPC file download
    using System.Threading.Tasks;
    
    
    using Grpc.Net.Client;
    
    
    using GrpcGreeterClient;
    
    
    
    
    // The port number must match the port of the gRPC server.
    using var channel = GrpcChannel.ForAddress("https://localhost:7092");
    
    
    var client = new Greeter.GreeterClient(channel);
    
    
    var reply = await client.SayHelloAsync(new HelloRequest { FileName = "test.txt" });
    
        
    File.WriteAllText(@"D:\files\"+"received"+reply.FileName, reply.Data.ToStringUtf8());
    
    
    Console.WriteLine("File written successfully. Press any key to exit...");
    
    
    Console.ReadKey();
like image 66
Kedar Avatar answered Nov 08 '25 19:11

Kedar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!