Upload large files to SharePoint Online using AddUsingPath

Sushrut Paranjape 61 Reputation points
2021-01-25T13:24:03.007+00:00

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.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,946 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,110 questions
0 comments No comments
{count} votes

Accepted answer
  1. MichaelHan-MSFT 18,046 Reputation points
    2021-01-26T08:45:39.153+00:00

    Hi @Sushrut Paranjape ,

    You need to add uploadFile = web.GetFileByServerRelativePath(resourcePath); under else if(continue):

    else if(continue)  
    {  
    	uploadFile = web.GetFileByServerRelativePath(resourcePath)  
    	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;  
    	}  
    }  
    

    My demo for you: https://github.com/hb1546297019/michael/blob/master/uploadLargeFilesCSOM.cs


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.