Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 包含重新安装的多部分内容_C#_.net_Refit - Fatal编程技术网

C# 包含重新安装的多部分内容

C# 包含重新安装的多部分内容,c#,.net,refit,C#,.net,Refit,我使用的是带有改装的multipart。我尝试上传我的服务的个人资料图片 由postman生成的代码如下所示 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-ca

我使用的是带有改装的multipart。我尝试上传我的服务的个人资料图片 由postman生成的代码如下所示

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);
[Multipart]
[Post("/users/{id}")]
IObservable<BaseResponse<User>> UpdateProfilePicture(int id,[AliasAs("profile_picture")] byte[] profilePicture,[AliasAs("_method")]string method="put");
然后我构造了这样的改装方法

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);
[Multipart]
[Post("/users/{id}")]
IObservable<BaseResponse<User>> UpdateProfilePicture(int id,[AliasAs("profile_picture")] byte[] profilePicture,[AliasAs("_method")]string method="put");
[多部分]
[Post(“/users/{id}”)]
IObservable UpdateProfilePicture(int-id,[AliasAs(“profile\u-picture”)]byte[]profilePicture,[AliasAs(“\u-method”)]string-method=“put”);
如果我使用
byte[]
ByteArrayPart
它将抛出异常

{System.Net.Http.HttpRequestException:发送时出错 请求-->System.Net.WebException:获取响应时出错 流(分块读取2):ReceiveFailure->System。异常:在 System.Net.WebConnection.HandleError(System.Net.WebExceptionStatus st,System.Exception e,System.String,其中[0x00031]位于 :0 at System.Net.WebConnection.Read(System.Net.HttpWebRequest请求, System.Byte[]缓冲区,System.Int32偏移量,System.Int32大小) [0x000d2]英寸:0英寸 中的System.Net.WebConnectionStream.ReadAll()[0x0010e] :0 at 中的System.Net.HttpWebResponse.ReadAll()[0x00011] :0 at System.Net.HttpWebRequest.CheckFinalStatus(System.Net.WebAsyncResult 结果)[0x001d6]在:0处 System.Net.HttpWebRequest.SetResponseData (System.Net.WebConnectionData数据)[0x0013e]中的 :0 at System.Net.WebConnection.ReadDone(System.IAsyncResult结果) [0x0024d]输入:0位于 System.Net.Sockets.SocketAsyncResult+c.b\u 27\u 0 中的(System.Object状态)[0x00000] :0 at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem ()[0x00015]英寸:0英寸 中的System.Threading.ThreadPoolWorkQueue.Dispatch()[0x00074] :0 at ObjCRuntime.Runtime.ThreadPoolDispatcher(System.Func`1[TResult] 回调)[0x00006]位于:0处 System.Threading.\u ThreadPoolWaitCallback.PerformWaitCallback() [0x00009]英寸:0英寸 System.Net.WebConnection.HandleError(System.Net.WebExceptionStatus st,System.Exception e,System.String,其中[0x00031]位于 /Library/Frameworks/Xamarin.iOS.framework/Versions/11.12.0.4/src/Xamarin.iOS/mcs/class/System/System.Net/WebConnection.cs:439


。如果我使用
流部分
也会抛出异常,表示流已关闭。

您可以使用
IEnumerable
上载文件:

  [Multipart]
  [Post("/users/{id}")]
  Task UpdateProfilePicture(int id, [AliasAs("profile_picture")] IEnumerable<StreamPart> streams);
[多部分]
[Post(“/users/{id}”)]
任务更新profilepicture(int-id,[AliasAs(“profile_-picture”)]IEnumerable streams);

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

我也无法使用
StreamPart
,因为服务器端接收到的文件大小始终为0,所以我使用
ByteArrayPart
来代替此处指定的文件大小

界面:

[Multipart]
[Post("/api/<some-path>")]
Task<HttpResponseMessage> Upload([AliasAs("file")] ByteArrayPart bytes);
var stream;
using (MemoryStream ms = new MemoryStream())
{
    stream.CopyTo(ms);
    client.Upload(new ByteArrayPart(ms.ToArray(), fileName));
}
请注意,此技术将在内存中加载整个流! 它对我很有用,因为我有小文件,但是如果你处理大文件或者内存使用是个问题,你应该找到一个更好的方法


我不需要上传很多文件。我只想上传一个文件。使用
StreamPart
会导致错误是的。已经在@hossein阅读了。仍然像我上面描述的那样出现异常