Reputation: 889
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
Upvotes: 9
Views: 1317
Reputation: 582
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);
}
}
Upvotes: 2
Reputation: 23
Have you tried uploading the file to the SharePoint document library from the SharePoint UI ? Does it work well ? It could be that the SharePoint site could be configured to disallow certain file types.
Also, are files uploaded to SharePoint expected to be retrieved using the CSOM model ?
Upvotes: 0
Reputation: 35554
I have no specific knowledge of SharePoint. But with websites and IIS if an extension is not listed under MIME Types
, then the browser will throw a 404.
It would seem that SharePoint has something similar. See
https://kerseub.wordpress.com/2012/04/23/add-new-file-type-in-sharepoint/
and
Upvotes: 0