我正在实施一个Xamarin Form移动应用,允许使用服务模块节点资源将照片发布到Drupal。 http://xxxx.com/ {端点} /节点/ 4 / attach_file
我尝试使用c#HttpClient实现,但仍然得到响应,例如" 401:缺少必需的参数field_name"
请帮助我的代码:
var httpClient = new HttpClient(new NativeMessageHandler());
httpClient.Timeout.Add(new TimeSpan(0, 0, 30));
httpClient.BaseAddress = new Uri(BaseAddress);
var content = new MultipartFormDataContent();
var streamContent = new StreamContent(g_media.GetStream());
streamContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse("form-data");
streamContent.Headers.ContentDisposition.Parameters.Add(new NameValueHeaderValue("field_name", "field_receipt_image"));
content.Add(streamContent,"files[file]");
var response = await httpClient.PostAsync("node/4/attach_file", content);
response.EnsureSuccessStatusCode();
答案 0 :(得分:0)
我遇到了同样的问题,并使用RestSharp解决了它。这是我用来将文件上传到Drupal的代码:
var restClient = new RestClient("http:XXXXXX/attach_file");
var request = new RestRequest(Method.POST);
request.AddFile("files[file]", fileName);
request.AddParameter("field_name", field);
IRestResponse response = restClient.Execute(request);