我有一个很大的文件(仅用于流式传输),并且想将其上传到我的REST WCF服务中:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "upload?name={name}&tags={tags}&storage={storage}")]
UploadFileResponse UploadFile(string name, string tags, string storage, Stream file);
这是我使用RestSharp的方法:
private void Upload(string filePath)
{
var name = Path.GetFileName(filePath);
var client = new RestClient(_endpoint);
client.Proxy = null;
var req = new RestRequest("upload", Method.POST);
req.AlwaysMultipartFormData = true;
req.AddQueryParameter("name", name);
if (_tags.Any())
{
req.AddQueryParameter("tags", string.Join(";", _tags));
}
req.AddQueryParameter("storage", _targetStorage);
//req.AddFile(name, filePath, "application/x-binary");
req.Files.Add(new FileParameter
{
Name = "file",
Writer = (s) =>
{
using (var stream = File.OpenRead(filePath))
{
stream.CopyTo(s);
}
},
FileName = name,
ContentLength = new FileInfo(filePath).Length,
});
var response = client.Execute<UploadFileResponse>(req);
response.ThrowIfError();
var result = response.Data;
}
这是我在发送简单文本文件时从服务器上的流中获取的信息:
-------------------------------28947758029299
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream
tttttttttttttttt <==== this is text I sent
-------------------------------28947758029299--
如何删除此多部分垃圾内容?是否存在某种可以读取此格式的流包装器?我应该如何处理呢?