我正在尝试使用Core.LargeFileUpload
将大于2MB的文件上传到SharePoint365。
我已对其进行更改以适合我的代码,但找不到我的文件夹。我收到错误响应“ 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字节文件被写入列表,而不是我想要的文件夹。
答案 0 :(得分:0)
我使用GetFileByURL代替了GetFileByServerRelativeURL
string furl = vURL + "/" + vList + "/" + vFolder + "/" + vFileName;
// Get a reference to our file
uploadFile = clientContext.Web.GetFileByUrl(furl);