wpf 以编程方式将 > 5 MB 的文件上传到 sharepoint 2013

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22052347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:01:59  来源:igfitidea点击:

Upload > 5 MB files to sharepoint 2013 programmatically

c#wpfsharepointsharepoint-2010sharepoint-2013

提问by B-M

I am having troubles uploading large files to my sharepoint 2013/office 365 site. I am using Visual Stuidos 2010 and .NET 4.0

我无法将大文件上传到我的 sharepoint 2013/office 365 站点。我使用的是 Visual Stuidos 2010 和 .NET 4.0

I have tried code from these questions:

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

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

SP2010 客户端对象模型 3 MB 限制 - 未应用更新 maxReceivedMessageSize

maximum file upload size in sharepoint

sharepoint 中的最大文件上传大小

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

通过 c# Web 服务将大文件 100mb+ 上传到 Sharepoint 2010

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

如何使用 CSOM 从/向 SharePoint 2013 下载/上传文件?

But nothing is working. So I need a little help. Here is code that I have tried:

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

1: ( I have also tried to use SharePointOnlineCredentialsinstead of NetworkCredentialfor this one)

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:

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

I have gotten smaller file uploads to work with:

我已经上传了较小的文件来处理:

#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

My Using Statement:

我的使用声明:

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
}

采纳答案by B-M

The solution I went with:

我采用的解决方案:

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

回答by Warlock

The problem with your code snippet number 2 is that you missed a file name:

您的代码片段编号 2 的问题是您错过了一个文件名:

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

回答by Amicable

My research on the subject showed that using the FrontPage Remote Control Procedureswas the most adventageous way of reliably uploading large files.

我对这个主题的研究表明,使用FrontPage 远程控制程序是可靠上传大文件的最有利的方式。

This is because FrontPage RPC supports file fragmentation, which helps avoid OutOfMemomoryexceptions due to Windows needing to allocate the entire file to continuous memory.

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

It also supports sending meta data, useful it pretty much any file upload application. One major advantage of this is that you can actually specify the correct content type without a user having to log in and change it later. (with all other methods I tried it would just be set as the default type.)

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

See my answer on the Sharepoint StackExchangefor further detail on implementing Frontpage RPC.

有关实施 Frontpage RPC 的更多详细信息,请参阅我在 Sharepoint StackExchange 上的回答