RestSharp is loading the whole file into memory wh

2019-03-27 13:47发布

问题:

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.

回答1:

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.



回答2:

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.