C
C#2mo ago
Arcanox

Mobile - Upload file over HTTP with reliable progress tracking

Is there a consistent way to send an HTTP POST request in MAUI that allows tracking progress? I have an implementation that uses a custom Stream class that wraps an inner Stream and reports read/write progress. I have attempted to consume this both in a StreamContent that is part of a MultipartFormDataContent, as well as by using the wrapper stream directly in a custom subclass of MultipartFormDataContent. No matter what I do, it seems like (on Android at least) the entire request is buffered into device memory before the request actually gets sent. Every request consumes the stream to 100% instantly and then hangs at 100% for as long as it takes the entire file to upload. I have tried putting <UseNativeHttpHandler>false</UseNativeHttpHandler> in the csproj file, and I've also tried explicitly using SocketsHttpHandler for the HttpClient instance. I've even tried explicitly setting request.Content.Headers.ContentLength = null before sending the request to force a chunked transfer. It seems like nothing I do will cause the request to actually be sent in a way that lets me track the progress as the request is transmitted. For reference, the amount of data that is being uploaded is around 1-5 MB per upload, but on slower mobile data connections, this can take 5-10 seconds for each request (meaning the user sees the progress bar jump to 100% and sit there for 5-10 seconds, which is a terrible experience). I would really rather not have to write platform-specific HTTP calls if I can avoid it, because I have common-layer code to handle things like authentication, logging, timeouts, and errors that I would not be able to use with platform-specific HTTP code.
2 Replies
Arcanox
ArcanoxOP2mo ago
Made some progress...by forcefully calling Flush or FlushAsync after every call to the inner stream's Write from the progress-tracking stream, and then by explicitly setting a small buffer size on the StreamContent for the actual file portion of the request, I am getting some intermediate progress updates. It still hangs at 100% for a little bit, but the progress reports seem to line up a bit better with actual upload progress now
cashen95
cashen952mo ago
How much if any control do you have over the endpoint?

Did you find this page helpful?