使用流的RestSharp AddFile [英] RestSharp AddFile Using Stream
问题描述
我正在使用RestSharp(Visual Studio 2013中的版本105.2.3.0,.net 4.5)来调用NodeJS托管的Web服务.我需要拨打的电话之一是上传文件.使用RESTSharp请求,如果我从末尾将流检索到字节数组中并将其传递给AddFile,它将正常工作.但是,我更希望流式传输内容,而不是将整个文件加载到服务器内存中(这些文件可以是100 MB的MB).
I am using RestSharp (version 105.2.3.0 in Visual Studio 2013, .net 4.5) to call a NodeJS hosted webservice. One of the calls I need to make is to upload a file. Using a RESTSharp request, if I retrieve the stream from my end into a byte array and pass that to AddFile, it works fine. However, I would much rather stream the contents and not load up entire files in server memory (the files can be 100's of MB).
如果我设置了一个操作来复制我的流(请参见下文),则会在System.Net.ProtocolViolationException的"MyStream.CopyTo"行得到一个异常(要写入流的字节超过了Content-Length字节)指定的尺寸).调用client.Execute之后,将在Action块内引发此异常.
If I set up an Action to copy my stream (see below), I get an exception at the "MyStream.CopyTo" line of System.Net.ProtocolViolationException (Bytes to be written to the stream exceed the Content-Length bytes size specified). This exception is thrown within the Action block after client.Execute is called.
从我读到的内容来看,我不应该手动添加Content-Length标头,如果这样做的话也无济于事.我尝试将CopyTo设置为太小和太大的值,并完全忽略它,但无济于事.有人可以提示我我错过了什么吗?
From what I read, I should not be manually adding a Content-Length header, and it doesn't help if I do. I have tried setting CopyTo buffer too small and large values, as well as omitting it entirely, to no avail. Can somebody give me a hint on what I've missed?
// 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...
}
推荐答案
我遇到了同样的问题.我最终在Files集合上使用了.Add().它具有FileParameter参数,该参数与AddFile()具有相同的参数,只需添加ContentLength:
I had the same issue. I ended up using the .Add() on the Files collection. It has a FileParameter param which has the same params as AddFile(), you just have to add the 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
});
这篇关于使用流的RestSharp AddFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!