【问题标题】:Upload file larger than 2Gb by restsharp通过restsharp上传大于2Gb的文件
【发布时间】:2021-11-09 17:08:46
【问题描述】:

我必须上传大于 2GB 的文件。

我想使用 restsharp,但出现错误“流太长”。 可以用 restharp 来做,还是我应该使用其他库?

            var client = new RestClient(url);
            client.ThrowOnDeserializationError = true;
            client.ConfigureWebRequest(x => x.AllowWriteStreamBuffering = false);
            var request = new RestRequest(Method.POST);
            request.AddHeader("X-Auth-Token", token);
            request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");

            const int chunkSize = 1024; // read the file by chunks of 1KB
            using (var file = File.OpenRead(filePath))
            {
                int bytesRead;
                var buffer = new byte[chunkSize];
                while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
                {
                    request.AddFileBytes("file", buffer, filePath);
                }
            }


            IRestResponse response = client.Execute(request);
            Console.WriteLine(response.Content);

【问题讨论】:

    标签: c# httprequest restsharp


    【解决方案1】:

    如果我们在 GitHub 上查看项目,项目https://github.com/restsharp/RestSharp 中不存在“Stream was too long”的文字

    相反,它通常是最大大小限制为 2 GB 的内存流的例外,不幸的是,RestSharp 在一种方法中使用 MemoryStream,但您应该能够使用另一种方法:

    //RestSharp.RestRequest.AddFileBytes:     will not work
    /// <inheritdoc />
    public IRestRequest AddFileBytes(
        string name,
        byte[] bytes,
        string filename,
        string contentType = "application/x-gzip"
    )
    {
        long length = bytes.Length;
    
        return AddFile(
            new FileParameter
            {
                Name          = name,
                FileName      = filename,
                ContentLength = length,
                ContentType   = contentType,
                Writer = s =>
                {
                    using var file = new StreamReader(new MemoryStream(bytes));
    
                    file.BaseStream.CopyTo(s);
                }
            }
        );
    }
    

    直接使用字节数组应该可以工作,所以使用 AddFile 而不是 AddFileBytes

    //RestSharp.RestRequest.AddFile  
    
    /// <inheritdoc />
    public IRestRequest AddFile(string name, byte[] bytes, string fileName, string contentType = null)
        => AddFile(FileParameter.Create(name, bytes, fileName, contentType));
    
    public static FileParameter Create(string name, byte[] data, string filename, string? contentType)
        => new() {
                Writer        = s => s.Write(data, 0, data.Length),
                FileName      = filename,
                ContentType   = contentType,
                ContentLength = data.LongLength,
                Name          = name
            };
    

    所以你看到后者使用基流类,这没有限制,例如 FileStream 派生可以处理更大的

    【讨论】:

    • 您当然可以采取简单的方法并将其作为项目中的错误提交,并希望有人会为您修复它:)
    猜你喜欢
    • 2018-09-21
    • 2011-06-04
    • 1970-01-01
    • 2013-05-07
    • 2016-02-17
    • 1970-01-01
    • 2020-09-16
    • 2014-11-03
    • 1970-01-01
    相关资源
    最近更新 更多