問題描述
我正在使用 RestSharp(Visual Studio 2013 中的版本 105.2.3.0,.net 4.5)來調用 NodeJS 托管的 Web 服務.我需要撥打的電話之一是上傳文件.使用 RESTSharp 請求,如果我將流從我的一端檢索到一個字節數組中并將其傳遞給 AddFile,它就可以正常工作.但是,我更愿意流式傳輸內容,而不是將整個文件加載到服務器內存中(文件可以是 100 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() .它有一個與 AddFile() 具有相同參數的 FileParameter 參數,您只需添加 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 使用 Stream的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!