简体   繁体   English

在C#中通过HTTP / REST发布大型文件,而无需将文件加载到内存中

[英]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). 但是,我看到的所有C#示例通常都使用字节数组(要求将整个文件加载到内存的内容中)。 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. 我可以看到有一个StreamContent类型,但我不清楚将其嵌入多部分上传中将以最小的内存影响流式传输请求。

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方式进行操作会更有效。 FTP code also supports resume mode. FTP代码还支持恢复模式。 And is more efficient protocol for file uplaod 并且是用于文件更新的更有效协议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 C# 和 openXML SDK 写入大型 docX 而不将其加载到内存文件中 - Write to the large docX without loading it in memory file using C# and openXML SDK 通过C#使用REST api将大文件放入一个驱动器 - Put large file to one drive with REST api via c# c#更改文件编码而不加载内存中的所有文件 - c# change file encoding without loading all the file in memory 在C#中,将大文件加载到winform richtextbox中 - In C#, Loading large file into winform richtextbox 在C#中加载大型JSON文件 - Loading large JSON file in C# C# - 将大文件加载到WPF RichTextBox中? - C# - Loading a large file into a WPF RichTextBox? 无法通过wwwroot C#加载静态文件 - Static file not loading via wwwroot c# 通过OleDBDataReader从大型Excel文件进​​行C#批量复制,抛出内存不足异常 - C# Bulk Copy via OleDBDataReader from large excel file throwing out of memory exception C#将字节数组追加到文件的开头(不将整个文件加载到内存中) - C# Append byte array to beginning of a file (without loading entire file in memory) 如何在不加载到内存的情况下对大型csv文件进行排序 - How can i sort large csv file without loading to memory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM