SharePoint 2013-文档库上传c#

时间:2018-09-17 18:17:35

标签: c# .net sharepoint-2013

我正在尝试使用c#将视频上传到SharePoint 2013文档库,每次代码运行时,我都会收到“找不到文件”异常,它只会对.mp4文件引发错误。如果我为此上传jpg或任何其他文件格式,它将可以使用。我究竟做错了什么?预先感谢您的帮助。

        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)
        {

        }

更新 创建了一个供人们下载的库,该库可用于上传.mp4 https://github.com/bikecrazyy/sharepoint-library-uploader

3 个答案:

答案 0 :(得分:2)

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);
 }

答案 1 :(得分:0)

我对SharePoint没有特定的了解。但是对于网站和IIS,如果扩展名未在MIME Types下列出,则浏览器将抛出404。

SharePoint似乎有类似的东西。参见

https://kerseub.wordpress.com/2012/04/23/add-new-file-type-in-sharepoint/

https://social.technet.microsoft.com/Forums/office/en-US/ef4a0922-bacd-40df-9c97-25d09bed30ac/how-to-play-mp4-video-in-sharepoint-2013?forum=sharepointgeneral

答案 2 :(得分:0)

您是否尝试过从SharePoint UI将文件上传到SharePoint文档库?它运作良好吗?可能是SharePoint网站可以配置为禁止某些文件类型。

https://support.office.com/en-us/article/types-of-files-that-cannot-be-added-to-a-list-or-library-30be234d-e551-4c2a-8de8-f8546ffbf5b3#ID0EAABAAA=2013,_2010

此外,是否希望使用CSOM模型检索上传到SharePoint的文件?