Provider Hosted App – Upload Large Files using CSOM
Now the method to upload larger files is exactly the same. We only have to use FileCreationInformation.ContentStream property instead of FileCreationInformation.Contentproperty. The whole code is present below:
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext, SPHostUrl);
using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())
{
if (clientContext != null)
{
//file is uploaded file. Could be read from Request.Files
using (FileStream fs = new FileStream(file.FileName, FileMode.Open))
{
//code for file size > 2MB. tested upto 50MB
FileCreationInformation newFile = new FileCreationInformation();
newFile.ContentStream = fs;
newFile.Url = Path.GetFileName(file.FileName);
newFile.Overwrite = true;
List docs = clientContext.Web.Lists.GetByTitle("Your List Name");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
clientContext.Load(uploadFile);
clientContext.ExecuteQuery();
}
}
}
In some cases, you might get a FileNotFoundException while using above code. Below is another way to use the code
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext, SPHostUrl);
using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())
{
if (clientContext != null)
{
FileCreationInformation newFile = new FileCreationInformation();
//here file is the selected file. In this case of type HttpPostedFileBase
newFile.ContentStream = file.InputStream;
newFile.Url = Path.GetFileName(file.FileName);
newFile.Overwrite = true;
List docs = clientContext.Web.Lists.GetByTitle("List_Name");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
clientContext.Load(uploadFile);
clientContext.ExecuteQuery();
}
}
Note that I replaced FileStream with the input stream present in the uploaded file itself.
Note that I have tested the above code for files upto 20 MB. Once for 20MB file size it gave me time out error (which could be fixed by setting RequestTimeout property of client context)
Hope this helps.
reference:
reference:
https://mysharepointlearnings.wordpress.com/2014/06/30/provider-hosted-app-upload-large-files-using-csom/
It is in point of fact a great and helpful piece of info. I am happy that you shared this useful info with us. Please stay us up to date like this. Thanks for sharing.
ReplyDeleteSharePoint Online Training
on using the below code it says access denied to specified Path Please help in this we are trying to save file more than 2 mb in ProviderHosted APP
ReplyDeleteusing (FileStream fs = new FileStream(fileName, FileMode.Open))
{
FileCreationInformation fileInfo = new FileCreationInformation();
fileInfo.ContentStream = fs;
fileInfo.Url = uniqueFileName;
fileInfo.Overwrite = true;
uploadFile = docs.RootFolder.Files.Add(fileInfo);
ctx.Load(uploadFile);
ctx.ExecuteQuery();
// Return the file object for the uploaded file.
return uploadFile;
}
Can u you try with the below
ReplyDeleteusing (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None))
I hope that all necessary permission was given in the manifest.xml. If not pls check