Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp is loading the whole file into memory when uploading. How to avoid it?

I'm using RestSharp in a Mono project to upload some files and I have noticed that when a large file is uploaded, the memory grows substantially.

Looking at RestSharp source code I did notice that FileParameter expects a byte array, which means it is really loading the file into memory.

Am I doing something wrong? Is there a way for RestSharp not do this? I might be uploading really large files so, uploading them from memory is not an option.

Any help (including telling me to use another HTTP library available on mono) is welcome.

like image 618
Maurício Linhares Avatar asked Jan 24 '12 14:01

Maurício Linhares


2 Answers

Use the AddFile(name, writer, filename) overload.

For the writer parameter, pass an Action<Stream> that writes directly to the request body stream. Do not close the stream.

Here's an example for writing to the stream.

like image 70
John Sheehan Avatar answered Nov 15 '22 22:11

John Sheehan


And I gave up after I found this line, so request bodies are always loaded into memory, which is unfortunate, so I built a simple solution to do file uploads based on code from this question and from debugging the Apache HttpClient library.

In case someone is interested, the source is available here.

like image 20
Maurício Linhares Avatar answered Nov 15 '22 23:11

Maurício Linhares