Я пытаюсь загрузить файлы размером более 2 МБ в SharePoint365, используя Core.LargeFileUpload
.
Я изменил его так, чтобы он соответствовал моему коду, но он не может найти мою папку. Я получаю сообщение об ошибке 'File Not Found
' и файл 0 байт записывается в список
Я пытался использовать:
Folder folder = docs.RootFolder.Folders.GetByUrl(vFolder);
безрезультатно, лучше всего получить 0-байтовый файл в папке, которую я хочу, но ошибка по-прежнему «Файл не найден»
using Microsoft.SharePoint.Client;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace UploadSH365
{
public class FileUploadService
{
public string Upload(string vURL, out string vURLout, string vUName, string vPWD, string vList, string vFolder, string vFileName, byte[] vAttachment, Dictionary<string, string> metadata, bool overwrite)
{
string strMsg = "";
vURLout = "";
if (string.IsNullOrEmpty(vURL))
return "Empty URL field";
int blockSize = 1 * 1024 * 1024;
try
{
long fileSize = vAttachment.Length;
// Microsoft.SharePoint.Client.File uploadFile;
Guid uploadId = Guid.NewGuid();
// Ensure that target library exists, create if is missing
using (ClientContext clientContext = new ClientContext(vURL))
{
SecureString passWord = new SecureString();
foreach (char c in vPWD.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(vUName, passWord);
Web web = clientContext.Web;
List docs = clientContext.Web.Lists.GetByTitle(vList);
clientContext.Load(docs, l => l.RootFolder);
// Get the information about the folder that will hold the file
clientContext.Load(docs.RootFolder, f => f.ServerRelativeUrl);
clientContext.ExecuteQuery();
// File object
Microsoft.SharePoint.Client.File uploadFile;
// Calculate block size in bytes
// int blockSize = fileChunkSizeInMB * 1024 * 1024;
// Get the information about the folder that will hold the file
clientContext.Load(docs.RootFolder, f => f.ServerRelativeUrl);
clientContext.ExecuteQuery();
;
// Use large file upload approach
ClientResult<long> bytesUploaded = null;
Stream fs = null;
try
{
fs = new MemoryStream(vAttachment);
using (BinaryReader br = new BinaryReader(fs))
{
byte[] buffer = new byte[blockSize];
Byte[] lastBuffer = null;
long fileoffset = 0;
long totalBytesRead = 0;
int bytesRead;
bool first = true;
bool last = false;
// Read data from filesystem in blocks
while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytesRead = totalBytesRead + bytesRead;
// We've reached the end of the file
if (totalBytesRead == fileSize)
{
last = true;
// Copy to a new buffer that has the correct size
lastBuffer = new byte[bytesRead];
Array.Copy(buffer, 0, lastBuffer, 0, bytesRead);
}
if (first)
{
using (MemoryStream contentStream = new MemoryStream())
{
// Add an empty file.
FileCreationInformation fileInfo = new FileCreationInformation();
fileInfo.ContentStream = contentStream;
fileInfo.Url = vFileName;
fileInfo.Overwrite = true;
uploadFile = docs.RootFolder.Files.Add(fileInfo);
// Start upload by uploading the first slice.
using (MemoryStream s = new MemoryStream(buffer))
{
// Call the start upload method on the first slice
bytesUploaded = uploadFile.StartUpload(uploadId, s);
clientContext.ExecuteQuery();
// fileoffset is the pointer where the next slice will be added
fileoffset = bytesUploaded.Value;
}
// we can only start the upload once
first = false;
}
}
else
{
// Get a reference to our file
uploadFile = clientContext.Web.GetFileByServerRelativeUrl(docs.RootFolder.ServerRelativeUrl + vFileName);
if (last)
{
// Is this the last slice of data?
using (MemoryStream s = new MemoryStream(lastBuffer))
{
// End sliced upload by calling FinishUpload
uploadFile = uploadFile.FinishUpload(uploadId, fileoffset, s);
clientContext.ExecuteQuery();
vURLout = vURL + "/" + vList + "/" + vFolder + "/" + vFileName;
// return the file object for the uploaded file
return vURLout;
}
}
else
{
using (MemoryStream s = new MemoryStream(buffer))
{
// Continue sliced upload
bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s);
clientContext.ExecuteQuery();
// update fileoffset for the next slice
fileoffset = bytesUploaded.Value;
}
}
}
} // while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
}
}
finally
{
if (fs != null)
{
fs.Dispose();
}
}
}
}
catch (Exception ex)
{
strMsg = ex.Message;
}
return strMsg;
// Console.ReadLine();
}
}
}
0 Байт-файл записывается в список вместо нужной мне папки.