I am trying to upload large files to SharePoint online using StartUpload, ContinueUpload and FinishUpload functions. This works fine for me when I use below code to add file:
using (MemoryStream contentStream = new MemoryStream())
{
FileCreationInformation fileInfo = new FileCreationInformation();
fileInfo.ContentStream = contentStream;
fileInfo.Url = uniqueFileName;
fileInfo.Overwrite = true;
uploadFile = parentFolder.Files.Add(fileInfo);
using (MemoryStream s = new MemoryStream(buffer10MB))
{
// Call the start upload method on the first slice.
bytesUploaded = uploadFile.StartUpload(uploadId, s);
ctx.ExecuteQuery();
// fileoffset is the pointer where the next slice will be added.
fileoffset = bytesUploaded.Value;
}
}
But I am trying to use Files.AddUsingPath function instead of Files.Add to allow special characters in the file name. Specifically I see that if the file name has % character then above code renames the file as %25 . But while using AddUsingPath I get error as:
Cannot access a closed Stream. at System.IO.__Error.StreamIsClosed() at System.IO.MemoryStream.get_Length() at Microsoft.SharePoint.Client.ClientRequest.WriteMimeStream(ExecuteQueryMimeInfo mimeInfo, ChunkStringBuilder sb, Stream requestStream) at Microsoft.SharePoint.Client.ClientRequest.SetupServerQuery(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb) at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
Code for AddUsingPath is as below:
if(first)
using (MemoryStream contentStream = new MemoryStream())
{
FileCollectionAddParameters fileAddParameters = new FileCollectionAddParameters();
fileAddParameters.Overwrite = true;
uploadFile = parentFolder.Files.AddUsingPath(resourcePath, fileAddParameters, contentStream);
using (MemoryStream s = new MemoryStream(buffer10MB))
{
// Call the start upload method on the first slice.
bytesUploaded = uploadFile.StartUpload(uploadId, s);
ctx.ExecuteQuery();
// fileoffset is the pointer where the next slice will be added.
fileoffset = bytesUploaded.Value;
}
}
else if(continue)
{
using (MemoryStream s = new MemoryStream(buffer10MB))
{
// Continue sliced upload.
bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s);
ctx.ExecuteQuery(); // Get error here Cannot access a closed Stream. when continue
// Update fileoffset for the next slice.
fileoffset = bytesUploaded.Value;
}
}
The difference I made here is add file using AddUsingPath if its first upload but continue upload and finish upload functionality remains the same.
Please let me know if I am missing something.