Jas*_*son 17 streaming upload restsharp
我正在使用RestSharp(Visual Studio 2013中的版本105.2.3.0,.net 4.5)来调用NodeJS托管的Web服务.我需要做的一个调用是上传文件.使用RESTSharp请求,如果我从我的端部检索流到字节数组并将其传递给AddFile,它工作正常.但是,我更倾向于流内容而不是在服务器内存中加载整个文件(文件可以是100的MB).
如果我设置一个动作来复制我的流(见下文),我在System.Net.ProtocolViolationException的"MyStream.CopyTo"行中得到一个异常(要写入流的字节超过指定的Content-Length字节大小) .在调用client.Execute之后,在Action块中抛出此异常.
根据我的阅读,我不应该手动添加Content-Length标头,如果我这样做,它也无济于事.我已经尝试将CopyTo缓冲区设置为小值和大值,以及完全省略它,但无济于事.有人能给我一些我错过的暗示吗?
// Snippet...
protected T PostFile<T>(string Resource, string FieldName, string FileName,
string ContentType, Stream MyStream,
IEnumerable<Parameter> Parameters = null) where T : new()
{
RestRequest request = new RestRequest(Resource);
request.Method = Method.POST;
if (Parameters != null)
{
// Note: parameters are all UrlSegment values
request.Parameters.AddRange(Parameters);
}
// _url, _username and _password are defined configuration variables
RestClient client = new RestClient(_url);
if (!string.IsNullOrEmpty(_username))
{
client.Authenticator = new HttpBasicAuthenticator(_username, _password);
}
/*
// Does not work, throws System.Net.ProtocolViolationException,
// Bytes to be written to the stream exceed the
// Content-Length bytes size specified.
request.AddFile(FieldName, (s) =>
{
MyStream.CopyTo(s);
MyStream.Flush();
}, FileName, ContentType);
*/
// This works, but has to load the whole file in memory
byte[] data = new byte[MyStream.Length];
MyStream.Read(data, 0, (int) MyStream.Length);
request.AddFile(FieldName, data, FileName, ContentType);
var response = client.Execute<T>(request);
// check response and continue...
}
Run Code Online (Sandbox Code Playgroud)
Ben*_*Ben 12
我遇到过同样的问题.我最终在Files集合上使用了.Add().它有一个FileParameter参数,它与AddFile()具有相同的参数,你只需要添加ContentLength:
var req = GetRestRequest("Upload", Method.POST, null);
//req.AddFile("file",
// (s) => {
// var stream = input(imageObject);
// stream.CopyTo(s);
// stream.Dispose();
// },
// fileName, contentType);
req.Files.Add(new FileParameter {
Name = "file",
Writer = (s) => {
var stream = input(imageObject);
stream.CopyTo(s);
stream.Dispose();
},
FileName = fileName,
ContentType = contentType,
ContentLength = contentLength
});
Run Code Online (Sandbox Code Playgroud)