I'm trying to upload video using c# to SharePoint 2013 document library, every time the code runs I get a "file not found" exception, it only throws errors with .mp4 files. If I upload a jpg or any other file format for that matter it will work. What am I doing wrong? Thanks in advance for the help.
string result = string.Empty;
try
{
//site url
ClientContext context = new ClientContext("siturl");
// The SharePoint web at the URL.
Web web = context.Web;
FileCreationInformation newFile = new FileCreationInformation();
newFile.Content = System.IO.File.ReadAllBytes(@"C:\test.mp4");
newFile.Url = "test.mp4";
List docs = web.Lists.GetByTitle("Learning Materials2");
Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile);
context.Load(uploadFile);
context.ExecuteQuery();
}catch(Exception Ex)
{
}
UPADTE Created a library, for people to download that will work for uploading .mp4's https://github.com/bikecrazyy/sharepoint-library-uploader
To upload a file to SharePoint 2013 Document Library: Open the Document Library. Navigate to Files > Upload document, or click the New Document button. Browse for a file on your desktop and click OK.
Make A Flow To Upload Documents To A SharePoint LibraryOpen the Power Automate action from the top menu and select Create a new flow. Choose the Power Apps button template. Name the flow UploadFileToDocument library and click Save.
You could check the permission of current user in the “Apps for SharePoint” library. Go to library settings->”Permissions for this document library” under “Permissions and Management”. Then you also could switch to another user to check if the same issue will occur.
string fileName=@"C:\test.mp4";
using (var clientContext = new ClientContext(siturl))
{
using (var fs = new FileStream(fileName, FileMode.Open))
{
var fi = new FileInfo(fileName);
var list = clientContext.Web.Lists.GetByTitle("Learning Materials2");
clientContext.Load(list.RootFolder);
clientContext.ExecuteQuery();
var fileUrl = String.Format("{0}/{1}", list.RootFolder.ServerRelativeUrl, fi.Name);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, fileUrl, fs, true);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With