Answered by:
Uploading bigger files using CSOM
-
Hi,
whenever i try to use this code
var myFile = new FileCreationInformation(); myFile.ContentStream = doc.ContentStream; using (ClientContext ctx = TokenHelper.GetClientContextWithAccessToken((string)request.Properties[hW], (string)request.Properties[aT])) { Folder destionationFolder = ctx.Web.GetFolderByServerRelativeUrl(destinationPath); ctx.Load(destionationFolder); ctx.ExecuteQuery(); File uploadedFile = destionationFolder.Files.Add(myFile); ctx.Load(uploadedFile); ctx.ExecuteQuery(); }I get the following error message:
"Specified argument was out of the range of valid values.\r\nParameter name: bytesToCopy"
Maybe there is another way to upload a file which is bigger than 2MB using the CSOM in combination with OAuth (SharePoint 2013/Office365?
best regards
Joerg
Question
Answers
-
I found a solution - it was a Problem with the MemoryStream:
var myFile = new FileCreationInformation();
using (var stream = content)
{
stream.Seek(0, SeekOrigin.Begin); <-- The missing statement
myFile.ContentStream = stream;
myFile.Url = FileName;
myFile.Overwrite = true;using (ctx)
{
Folder destionationFolder = ctx.Web.GetFolderByServerRelativeUrl(options.SPDestinationPath);
ctx.Load(destionationFolder);
ctx.ExecuteQuery();
File uploadedFile = destionationFolder.Files.Add(myFile);ctx.Load(uploadedFile);
ctx.ExecuteQuery();
}
}- Marked as answer by JoergKerschbaumer Monday, February 18, 2013 5:09 PM
- Edited by JoergKerschbaumer Monday, February 18, 2013 5:10 PM
All replies
-
-
Hi, You can try:
public static void UploadFile(SP.ClientContext context, SP.Folder folder, string name) { using (FileStream fileStream = new FileStream("C:\\NewDocument2.docx", FileMode.Open)) { SP.FileCreationInformation flciNewFile = new SP.FileCreationInformation(); flciNewFile.ContentStream = fileStream; flciNewFile.Url = name; flciNewFile.Overwrite = true; SP.File uploadFile = folder.Files.Add(flciNewFile); context.Load(uploadFile); context.ExecuteQuery(); } }
- Proposed as answer by Krzysztof Kordyga Friday, February 08, 2013 1:13 PM
-
Normally you can circumvent the 3MB limit by setting the MaxReceivedMessage size property that is described in http://gallery.technet.microsoft.com/The-Migration-Dragon-for-628acae0 That piece of code uses the server object model and you're not allowed to change this setting in Office 365. On premises, in 2013, you could use that. I built the tool for 2010, but here's the piece of sample code:
private void IncreaseMaxReceivedMessageSize() { int increaseSize = Convert.ToInt32(txtBatchSize.Text) * 1024 * 1024; SPWebService contentService = SPWebService.ContentService; contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = increaseSize; contentService.Update(); }Kind regards,
Margriet BruggemanLois & Clark IT Services
web site: http://www.loisandclark.eu
blog: http://www.sharepointdragons.com
-
You can use SaveBinaryDirect instead of file.add("File");
using (ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken((string)request.Properties[hW], (string)request.Properties[aT]))
{
Web web = clientContext.Web;
List documentsList = null;
documentsList = clientContext.Web.Lists.GetByTitle(pstrListTitle);
clientContext.Load(web, website => website.ServerRelativeUrl);
clientContext.Load(documentsList, DocList => DocList.ForceCheckout);
clientContext.ExecuteQuery();
var fileCreationInformation = new FileCreationInformation();
// Assign to content byte[] i.e. documentStream
fileCreationInformation.Content = System.IO.File.ReadAllBytes(filePath);
// Allow owerwrite of document
fileCreationInformation.Overwrite = true;
// Upload URL
fileCreationInformation.Url = fileURL;
using (FileStream fs = new FileStream(filePath, FileMode.Open))
{
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, strServerRelativeURL, fs, true);
}
clientContext.ExecuteQuery();
} -
I found a solution - it was a Problem with the MemoryStream:
var myFile = new FileCreationInformation();
using (var stream = content)
{
stream.Seek(0, SeekOrigin.Begin); <-- The missing statement
myFile.ContentStream = stream;
myFile.Url = FileName;
myFile.Overwrite = true;using (ctx)
{
Folder destionationFolder = ctx.Web.GetFolderByServerRelativeUrl(options.SPDestinationPath);
ctx.Load(destionationFolder);
ctx.ExecuteQuery();
File uploadedFile = destionationFolder.Files.Add(myFile);ctx.Load(uploadedFile);
ctx.ExecuteQuery();
}
}- Marked as answer by JoergKerschbaumer Monday, February 18, 2013 5:09 PM
- Edited by JoergKerschbaumer Monday, February 18, 2013 5:10 PM
-

