简体   繁体   中英

Posting a large file via HTTP/REST in C# without loading file into memory

I would like to upload a large file as a standard multipart post message. All the C# examples I see however typically use byte arrays (which require the entire file be loaded into the contents of memory). I can see that there is a StreamContent type but it isn't clear to me that embedding that in a multi part upload would stream the request with minimal memory impact.

Here's coding wise what I'm thinking.

using (HttpRequestMessage request = new HttpRequestMessage (HttpMethod.Post, uploadURL)) {
    MultipartFormDataContent multiPartContent = new MultipartFormDataContent ("----Abs23AawqrrqTbbSWpppo8--");
    StreamContent streamContent = new StreamContent (new FileStream(path, FileMode.Open));
    streamContent.Headers.Add ("Content-Type", "video/mov");
    streamContent.Headers.Add ("Content-Length", new FileInfo(path).Length.ToString() );
    multiPartContent.Add (streamContent, "bigMovie.mov", "bigMovie.mov");
    request.Content = multiPartContent;

    using (HttpResponseMessage response = await client.SendAsync (request)) {
        // check status code
    }
}

Is this the correct way to handle a multi part upload for a large file that would be too big to load into memory?

Doing it FTP way would be more efficient. FTP code also supports resume mode. And is more efficient protocol for file uplaod

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