多部分内容与改装

alb*_*aga 10 .net c# refit

我正在使用带有Refit的multipart.我尝试为我的服务上传个人资料图片,邮递员生成的代码看起来像这样

var client = new RestClient("http://api.example.com/api/users/1");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "xxx");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"_method\"\r\n\r\nput\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"profile_picture\"; filename=\"ic_default_avatar.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

然后我像这样构建Refit方法

[Multipart]
[Post("/users/{id}")]
IObservable<BaseResponse<User>> UpdateProfilePicture(int id,[AliasAs("profile_picture")] byte[] profilePicture,[AliasAs("_method")]string method="put");
Run Code Online (Sandbox Code Playgroud)

如果我使用byte[]ByteArrayPart它将抛出异常

{System.Net.Http.HttpRequestException:发送请求时发生错误---> System.Net.WebException:获取响应流时出错(chunked Read2):ReceiveFailure ---> System.Exception:at System.Net.WebConnection .HandleError(System.Net.WebExceptionStatus st,System.Exception e,System.String where)[0x00031] in:0 at System.Net.WebConnection.Read(System.Net.HttpWebRequest request,System.Byte [] buffer,System .Int32 offset,System.Int32 size)[0x000d2] in:0,System.Net.WebConnectionStream.ReadAll()[0x0010e] in:0,System.Net.HttpWebResponse.ReadAll()[0x00011] in:0 at System. Net.HttpWebRequest.CheckFinalStatus(System.Net.WebAsyncResult result)[0x001d6] in:0,System.Net.HttpWebRequest.SetResponseData(System.Net.WebConnectionData data)[0x0013e] in:0,System.Net.WebConnection.ReadDone( System.IAsyncResult结果)[0x0024d] in:0,System.Net.Sockets.SocketAsyncResult + <> c.b__27_0(System.Object state)[0x00000] in:0,System.Threading.QueueUserWorkItemCa llback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()[0x00015] in:0中的System.Threading.ThreadPoolWorkQueue.Dispatch()[0x00074] in:0 at ObjCRuntime.Runtime.ThreadPoolDispatcher(System.Func`1 [TResult] callback) System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()[0x00009]中的<0b60c1467e7449608ac42f9cbbbdd05>中的[0x00006]:0:System.Net.WebConnection.HandleError中的0:System.Net.WebExceptionStatus st,System.Exception e,System.String where )/Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:439中的[0x00031]

.如果我使用StreamStreamPart它也将抛出异常表示流已关闭.

Hos*_*ein 8

您可以使用IEnumerable<StreamPart>上传文件:

  [Multipart]
  [Post("/users/{id}")]
  Task UpdateProfilePicture(int id, [AliasAs("profile_picture")] IEnumerable<StreamPart> streams);
Run Code Online (Sandbox Code Playgroud)


Max*_*nas 6

这是一个老问题,但也许它可以帮助其他人。

我也无法使用StreamPart,因为在服务器端收到的文件大小始终为 0,所以我ByteArrayPart改为使用github.com/reactiveui/refit#multipart-uploads 中指定的方式

界面:

[Multipart]
[Post("/api/<some-path>")]
Task<HttpResponseMessage> Upload([AliasAs("file")] ByteArrayPart bytes);
Run Code Online (Sandbox Code Playgroud)

用法:

var stream;
using (MemoryStream ms = new MemoryStream())
{
    stream.CopyTo(ms);
    client.Upload(new ByteArrayPart(ms.ToArray(), fileName));
}
Run Code Online (Sandbox Code Playgroud)

请注意,此技术将在内存中加载整个流! 它对我有用,因为我有小文件,但是如果您正在处理大文件或内存使用问题,您应该找到更好的方法。