以下Java代码是将文件附件上载到SharePoint 2013列表项的示例 .
String uploadquery =siteurl+ _api/web/Lists/GetByTitle('ListName')/items(1)/AttachmentFiles/add(FileName='File1.txt')";
HttpPost httppost = new HttpPost(uploadquery);
httppost.addHeader("Accept", "application/json;odata=verbose");
httppost.addHeader("X-RequestDigest", FormDigestValue);
httppost.addHeader("X-HTTP-Method", "PUT");
httppost.addHeader("If-Match", "*");
StringEntity se = new StringEntity("This is a Body");
httppost.setEntity(se);
HttpResponse response = httpClient.execute(httppost, localContext);
它使用content创建文件 . 但它在响应中返回以下错误 .
{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The type SP.File does not support HTTP PATCH method."}}}
是什么导致这个问题?
在上面的代码中,我上传了简单的文本内容 . 但是如何将其他文件类型(如excel / ppt或图像)上传到sharepoint列表项?
2 回答
根据Working with folders and files with REST,必须指定以下属性才能创建列表项的文件附件:
POST
HTTP请求的方法带有
FormDigest
值的X-RequestDigest
标头HTTP请求
body
和content length
伪示例:
C#示例
以下C#示例演示如何使用Network API将创建文件附件上载到列表项到SharePoint Online(SPO):
我相信只需很少的努力就可以转换成Java版本 .
我使用了以下URL和相关代码为我工作