File Transfer
See original GitHub issueBlazor WASM, .NET5 I’m trying to transfer files to the server and back. I transmit parts of 3mb in the form of bytes. This consumes a lot of RAM by the browser. And when I transfer from the server, it happens very slowly for files larger than 3mb. Maybe there is a way to transfer a stream, not a byte? or maybe I have a failed approach?
proto
service Greeter {
rpc UploadFile(stream FileRequest) returns (StatusRequest);
rpc DownloadFile(FilesInfoRequest) returns (stream FileRequest)
}
message StatusRequest {
string Status = 1;
}
message FileRequest {
string FileName = 1;
bytes FileBytes =2;
}
message FilesInfoRequest {
string FileName = 1;
string FilePath = 2;
}
Sending method. Executed in the browser. Works fast. Google.Protobuf.UnsafeByteOperations.UnsafeWrap - consumes a lot of RAM ~ file 120mb - 1 gigabytes
private const int _maxSize = 3 * 1024 * 1024;
private byte[] _part = new byte[_maxSize];
private FileRequest _fileRequest;
private async void UploadFiles(InputFileChangeEventArgs e)
{
foreach (var file in e.GetMultipleFiles())
{
try
{
using var call = GreeterClient.UploadFile();
using (var ms = file.OpenReadStream(file.Size))
{
while (ms.Length - ms.Position > 0)
{
if (ms.Length - ms.Position <= _maxSize)
_part = new byte[ms.Length - ms.Position];
await ms.ReadAsync(_part, 0, _part.Length);
_fileRequest = new FileRequest { FileName = file.Name, FileBytes = Google.Protobuf.UnsafeByteOperations.UnsafeWrap(_part) };
await call.RequestStream.WriteAsync(_fileRequest);
_fileRequest = null;
}
}
await call.RequestStream.CompleteAsync();
var response = await call;
var status = response.Status;
}
catch(Exception ex)
{
ShowMessage(ex.Message, Severity.Error);
}
}
}
Method of getting the file from the server (download). Running on the server. It works very slowly. Almost does not consume RAM.
private const int _maxSize = 3 * 1024 * 1024;
private byte[] _part = new byte[_maxSize];
public override async Task DownloadFile(FilesInfoRequest request, IServerStreamWriter<FileRequest> responseStream, ServerCallContext context)
{
using (FileStream fstream = new FileStream(request.FilePath,FileMode.Open))
{
while (fstream.Length - fstream.Position > 0)
{
if (fstream.Length - fstream.Position <= _maxSize)
_part = new byte[fstream.Length - fstream.Position];
await fstream.ReadAsync(_part, 0, _part.Length);
await responseStream.WriteAsync(new FileRequest
{
FileName = fstream.Name,
FileBytes = UnsafeByteOperations.UnsafeWrap(_part)
});
}
}
}
What’s going wrong?
Issue Analytics
- State:
- Created 2 years ago
- Comments:11 (5 by maintainers)
Top Results From Across the Web
Fast and secure file transfer
Dropbox Transfer: Deliver files hassle-free. Securely transferring a large file or collection of files in real time—for example, a design project or set...
Read more >Android File Transfer
Browse and transfer files between your Mac computer and your Android device. Download now. For Mac OS X only. No extra software is...
Read more >WeTransfer - Send Large Files & Share Photos Online - Up to ...
WeTransfer is the simplest way to send your files around the world. Share large files and photos. Transfer up to 2GB free. File...
Read more >FileTransfer.io: Fast & Simple File Transfers
Free transfer of up to 6 GB of photos, videos and documents. Send large files via email or a link to share. No...
Read more >TransferNow: Send Large Files - Free Secure File Transfer
TransferNow is a simple, quick and secure free solution to send large files and big documents up to 200 GB per transfer. No...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
New docs that discuss file uploading with gRPC - https://docs.microsoft.com/en-us/aspnet/core/grpc/performance?view=aspnetcore-6.0#grpc-services-and-large-binary-payloads
I’m surprised this doesn’t error. gRPC-Web in Blazor WebAssembly doesn’t support client streaming. If it does work, then the client must be buffering all data before sending it. That would explain why you have high memory usage.
I’m not sure about the download slowness. Is this between apps on the same computer, or is there latency involved?