【问题标题】:Upload > 5 MB files to sharepoint 2013 programmatically以编程方式将 > 5 MB 的文件上传到 sharepoint 2013
【发布时间】:2014-03-29 22:16:18
【问题描述】:

我在将大文件上传到我的 sharepoint 2013/office 365 站点时遇到问题。我正在使用 Visual Stuidos 2010 和 .NET 4.0

我已经尝试了这些问题的代码:

SP2010 Client Object Model 3 MB limit - updating maxReceivedMessageSize doesnt get applied

maximum file upload size in sharepoint

Upload large files 100mb+ to Sharepoint 2010 via c# Web Service

How to download/upload files from/to SharePoint 2013 using CSOM?

但没有任何效果。所以我需要一点帮助。这是我尝试过的代码:

1:(我也试过用SharePointOnlineCredentials代替NetworkCredential这个)

#region 403 forbidden

byte[] content = System.IO.File.ReadAllBytes(fileInfo.FullName);
System.Net.WebClient webclient = new System.Net.WebClient();
System.Uri uri = new Uri(sharePointSite + directory + fileInfo.Name);
webclient.Credentials = new NetworkCredential(user, password.ToString(), sharePointSite + "Documents");

webclient.UploadData(uri, "PUT", content);

#endregion

2:

#region 500 Internal Server Error


using (var fs = new FileStream(fileInfo.FullName, FileMode.Open))
{
    Microsoft.SharePoint.Client.File.SaveBinaryDirect(
        context, 
        web.ServerRelativeUrl + "/" + directory, 
        fs, 
        true);
}

#endregion

我已经上传了更小的文件:

#region File upload for smaller files

Folder folder = context.Web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + directory);
web.Context.Load(folder);
context.ExecuteQuery();

FileCreationInformation fci = new FileCreationInformation();

fci.Content = System.IO.File.ReadAllBytes(fileInfo.FullName);

fciURL = sharePointSite + directory;
fciURL += (fciURL[fciURL.Length - 1] == '/') ? fileInfo.Name : "/" + fileInfo.Name;

fci.Url = fciURL;
fci.Overwrite = true;

Microsoft.SharePoint.Client.FileCollection documentfiles = folder.Files;
context.Load(documentfiles);
context.ExecuteQuery();

Microsoft.SharePoint.Client.File file = documentfiles.Add(fci);
context.Load(file);
context.ExecuteQuery();

#endregion

我的使用声明:

using (Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(sharePointSite))
{
    //string fciURL = "";
    exception = "";
    context.Credentials = new Microsoft.SharePoint.Client.SharePointOnlineCredentials(user, password);


    Web web = context.Web;
    web.Context.Credentials = context.Credentials;


    if (!web.IsPropertyAvailable("ServerRelativeUrl"))
    {
        web.Context.Load(web, w => w.ServerRelativeUrl);
        web.Context.ExecuteQuery();
    }

    //upload large file
}

【问题讨论】:

    标签: c# wpf sharepoint sharepoint-2010 sharepoint-2013


    【解决方案1】:

    我采用的解决方案:

    MemoryStream destStream;
    
    using (System.IO.FileStream fInfo = new FileStream(fileInfo.FullName, FileMode.Open))
    {
    
        byte[] buffer = new byte[16 * 1024];
        byte[] byteArr;
    
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = fInfo.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            byteArr = ms.ToArray();
        }
    
        destStream = new MemoryStream(byteArr);
    
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(
            context,
            serverRelativeURL + directory + fileInfo.Name,
            destStream,
            true);
    
        context.ExecuteQuery();
        results = "File Uploaded";
        return true;
    }
    

    【讨论】:

    • 你不需要调用context.ExecuteQuery()方法。
    • 我相信你应该删除所有与 MemoryStream 相关的方法,并将fInfo 流放到SaveBinaryDirect() 方法中。
    【解决方案2】:

    你的代码 sn -p number 2 的问题是你错过了一个文件名:

    using (var fs = new FileStream(fileInfo.FullName, FileMode.Open))
    {
        Microsoft.SharePoint.Client.File.SaveBinaryDirect(
            context, 
            serverRelativeURL + directory + fs.Name,
                                            ^^^^^^^^
            fs, 
            true);
    }
    

    【讨论】:

      【解决方案3】:

      我对该主题的研究表明,使用FrontPage Remote Control Procedures 是可靠上传大文件的最冒险的方式。

      这是因为 FrontPage RPC 支持文件碎片,这有助于避免由于 Windows 需要将整个文件分配到连续内存而导致的 OutOfMemomory 异常。

      它还支持发送元数据,几乎适用于任何文件上传应用程序。这样做的一个主要优点是,您实际上可以指定正确的内容类型,而无需用户稍后登录并更改它。 (使用我尝试的所有其他方法,它只会被设置为默认类型。)

      See my answer on the Sharepoint StackExchange 了解有关实施 Frontpage RPC 的更多详细信息。

      【讨论】:

        猜你喜欢
        • 2020-01-27
        • 2014-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-16
        • 2023-02-14
        • 1970-01-01
        相关资源
        最近更新 更多