![](/img/trans.png)
[英]How to upload a large document in c# using the Microsoft Graph API rest calls
[英]Microsoft Graph Api Upload Large File in a folder
我正在嘗試使用 LagFileUploadTask 將大於 4 mb 的文件上傳到 Sharepoint 列表中的文件夾中。 該代碼似乎在“城市”文件夾中的 PostAsync 之后創建臨時文件。 我看到創建了一個文件,即 ~tmphamilton.png 。 但是當 UploadAsync 被調用時它會失敗,並返回“找不到資源”
當我在創建 uploadSession 時刪除文件夾路徑時,代碼工作正常
知道我可能做錯了什么嗎?
// create an upload session
var uploadSession = await graphClient.Sites("site id").Drive().Root().ItemWithPath("cities\hamilton.png").CreateUploadSession().Request().PostAsync();
var maxSliceSize = 320 * 1024; // 320 KB - Change this to your slice size. 5MB is the default.
var largeFileUploadTask = new LargeFileUploadTask(uploadSession, stream, maxSliceSize);
// upload away with relevant callback
DriveItem itemResult = await largeFileUploadTask.UploadAsync( progress );
確保最大。 您的 WebApplication 中的上傳大小設置大於您的文件。 取決於上傳位置和 SharePoint 版本,它可能小於 50MB。 On Premise,可以在CentralAdmin->Application Management->Web Application xy->General Settings 進行設置
另請檢查https://docs.microsoft.com/de-de/archive/blogs/bgeoffro/list-attachments-over-50mb-need-more-than-an-increase-in-maximum-upload-size
您是否能夠將文件成功上傳到文檔庫的根目錄? 請參閱下面的代碼。
string siteId = WebConfigurationManager.AppSettings.Get("SiteId");
string libraryName = WebConfigurationManager.AppSettings.Get("LibraryName");
string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
var uploadSession = await graphServiceClient.Sites[siteId].Lists[libraryName].Drive.Root
.ItemWithPath(fileName)
.CreateUploadSession(uploadProps)
.Request()
.PostAsync();
它對我使用 Graph 3.1.4 和 Java 確實有效,我花了一段時間才讓它工作,但它上傳的文件 >4MB 性能相當好......
// itemPath 類似於“/myFolder/DailyUpload/”
public CompletableFuture UploadFileToSharePoint(String idSite, String parentId, String itemPath, String fileName, InputStream file) {
final List<Option> options = new LinkedList<Option>();
try {
return mClient
.sites(idSite)
.drive()
.root()
.itemWithPath(itemPath+ Build.ID.toUpperCase()+"/"+fileName)
.createUploadSession(
DriveItemCreateUploadSessionParameterSet.newBuilder()
.withItem(new DriveItemUploadableProperties())
.build())
.buildRequest(options)
.postAsync().thenCompose(uploadSession -> {
byte[] data = null;
try {
byte[] buff = new byte[file.available()];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((bytesRead = file.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
data = bao.toByteArray();
}
catch (IOException e) {
e.printStackTrace();
}
if (data != null && data.length>0) {
final InputStream uploadFile = new ByteArrayInputStream(data); //"{\"hehe\":\"haha\"}".getBytes(StandardCharsets.UTF_8));
try {
final long fileSize = (long) uploadFile.available();
LargeFileUploadTask<DriveItem> largeFileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession,
mClient, uploadFile, fileSize, DriveItem.class);
// If everything is fine, the procedure returns a DriveItem with the item already
return largeFileUploadTask.uploadAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
});
}
catch (IndexOutOfBoundsException e) {
Log.d("GraphHelper", "UploadFileToSharePoint failed: "+ e.getMessage());
return CompletableFuture.completedFuture(false);
}
}
希望這對其他人有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.