我希望能够发布一个文件,并作为该帖子的一部分添加数据.
这是我有的:
var restRequest = new RestRequest(Method.POST);
restRequest.Resource = "some-resource";
restRequest.RequestFormat = DataFormat.Json;
string request = JsonConvert.SerializeObject(model);
restRequest.AddParameter("text/json", request, ParameterType.RequestBody);
var fileModel = model as IHaveFileUrl;
var bytes = File.ReadAllBytes(fileModel.LocalStoreUrl);
restRequest.AddFile("FileData", bytes, "file.zip", "application/zip");
var async = RestClient.ExecuteAsync(restRequest, response =>
{
if (PostComplete != null)
PostComplete.Invoke(
new Object(),
new GotResponseEventArgs
<T>(response));
});
Run Code Online (Sandbox Code Playgroud)
它发布文件很好,但数据不存在 - 这甚至可能吗?
[UPDATE]
我修改了代码以使用多部分标题:
var restRequest = new RestRequest(Method.POST);
Type t = GetType();
Type g = t.GetGenericArguments()[0];
restRequest.Resource = string.Format("/{0}", g.Name);
restRequest.RequestFormat = DataFormat.Json;
restRequest.AddHeader("content-type", "multipart/form-data");
string …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过来自C#.net的web服务将新的报告单元上传到jasperserver我已经成功上传/创建了报告单元但是当我通过iReport资源库导航器点击报告时它显示"No Attachment Present!" 在一个弹出框中.下面是我发送给webservice的'createXML':
<request operationName='put' locale='en'>
<resourceDescriptor name='barunit' wsType='reportUnit'
uriString='/reports/bar/bar_files'
isNew='true'>
<label>Bar Unit</label>
<description>This is a test</description>
<resourceProperty name='PROP_PARENT_FOLDER'>
<value>/reports/bar</value>
</resourceProperty>
<resourceDescriptor name='bar.jrxml' wsType='jrxml'
uriString='/reports/bar/bar_files'
isNew='true'>
<label>Bar Report</label>
<description>This is a test</description>
<resourceProperty name='PROP_RU_IS_MAIN_REPORT'>
<value>true</value>
</resourceProperty>
</resourceDescriptor>
</resourceDescriptor>
</request>
Run Code Online (Sandbox Code Playgroud)
以下是将"createXML"发送到Web服务的代码:
JasperService.ManagementServiceService service = new JasperService.ManagementServiceService();
service.Credentials = new System.Net.NetworkCredential("jasperadmin", "jasperadmin");
service.PreAuthenticate = true;
FileStream fs = new FileStream(@"C:\bar.jrxml", FileMode.Open, FileAccess.Read);
Microsoft.Web.Services2.Attachments.Attachment jrxmlAttachment = new Microsoft.Web.Services2.Attachments.Attachment("text/xml",fs);
service.RequestSoapContext.Attachments.Add(jrxmlAttachment);
string out = service.put(createXML);
Run Code Online (Sandbox Code Playgroud)
来自webservice调用的响应给出了成功代码'0',所以我有点难过.我猜测问题在于RequestSoapContext的文件附件,因为在此之前一切都很好.
任何帮助将不胜感激!