简体   繁体   中英

gRPC c# file download/upload + web client side

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 eg 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

Please help or at least say that it is impossible

Yes it is possible to use gRPC to communicate between browser and a server. I would propose you to do a prove of concept before implementing the solution.

gRPC uses protobuf to communicate and the data that needs to be communicated is encapsulated in a protobuf message .

The first answer in gRPC Java File Download Example is correct. To be able to transfer files over a network you have to convert it into bytes .

Using his example,

message DataChunk {
   bytes data = 1;
}

service imageService {
   rpc DownloadProductImage(DownloadProductImageRequest) returns(stream DataChunk);
}

All this code should be in a protofile and the implementation should be generated using a protobuf generator (usually it is a maven plugin for java).

The important point to note in the example provided is the word stream , this is what will enable you to send bytes to the server or vice versa. In the example he is downloading a stream of bytes from the service call DownloadProductImage.

Here is a good starting point gRPC Java Basics , gRPC Concepts , Building a gRPC service with Java

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; }

  2. 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)) }); } }

    }

  3. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM