![](/img/trans.png)
[英]Upload image attachment to SharePoint Lsit Item using REST API - Java
[英]upload attachment to SharePoint List Item using REST API from Java client
以下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);
它創建具有內容的文件,但是在響應中返回以下錯誤。
{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The type SP.File does not support HTTP PATCH method."}}}
是什么原因導致此問題?
在上面的代碼中,我上傳了簡單的文本內容。但是如何將其他文件類型(例如excel / ppt或圖像)上傳到共享點列表項?
根據使用REST處理文件夾和文件,必須指定以下屬性來創建列表項的文件附件:
POST
方法 FormDigest
值的X-RequestDigest
標頭 body
和content length
偽示例:
url: http://site url/_api/web/lists/getbytitle('list title')/items(item id)/AttachmentFiles/ add(FileName='file name')
method: POST
headers:
Authorization: "Bearer " + accessToken
body: "Contents of file."
X-RequestDigest: form digest value
content-length:length of post body
以下C#示例演示了如何使用網絡API將創建文件附件上傳到列表項到SharePoint Online(SPO):
var fileName = Path.GetFileName(uploadFilePath);
var requestUrl = string.Format("{0}/_api/web/Lists/GetByTitle('{1}')/items({2})/AttachmentFiles/add(FileName='{3}')", webUrl, listTitle, itemId,fileName);
var request = (HttpWebRequest)WebRequest.Create(requestUrl);
request.Credentials = credentials; //SharePointOnlineCredentials object
request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
request.Method = "POST";
request.Headers.Add("X-RequestDigest", requestDigest);
var fileContent = System.IO.File.ReadAllBytes(uploadFilePath);
request.ContentLength = fileContent.Length;
using (var requestStream = request.GetRequestStream())
{
requestStream.Write(fileContent, 0, fileContent.Length);
}
var response = (HttpWebResponse)request.GetResponse();
我相信只要花費很少的精力就可以將其轉換為Java版本。
我已使用以下網址和以下相關代碼為我工作
https:// [您的域] .sharepoint.com / _api / web / GetFolderByServerRelativeUrl ('/ Shared%20Documents / [FolderName]')/ Files / Add(url ='“ + [FileName] +”',overwrite = true)
public String putRecordInSharePoint(File file) throws ClientProtocolException, IOException
{
/* Token variable declaration */
String token = getSharePointAccessToken();
/* Null or fail check */
if (!token.equalsIgnoreCase(RcConstants.OAUTH_FAIL_MESSAGE))
{
/* Upload path and file name declaration */
String Url_parameter = "Add(url='" + file.getName() + "',overwrite=true)";
String url = RcConstants.UPLOAD_FOLDER_URL + Url_parameter;
/* Building URL */
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
post.setHeader("Authorization", "Bearer " + token);
post.setHeader("accept", "application/json;odata=verbose");
/* Declaring File Entity */
post.setEntity(new FileEntity(file));
/* Executing the post request */
HttpResponse response = client.execute(post);
logger.debug("Response Code : " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()
|| response.getStatusLine().getStatusCode() == HttpStatus.ACCEPTED.value())
{
/* Returning Success Message */
return RcConstants.UPLOAD_SUCCESS_MESSAGE;
}
else
{
/* Returning Failure Message */
return RcConstants.UPLOAD_FAIL_MESSAGE;
}
}
return token;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.