Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C将文件上载到共享点#_C#_Sharepoint_File Upload - Fatal编程技术网

C# 使用C将文件上载到共享点#

C# 使用C将文件上载到共享点#,c#,sharepoint,file-upload,C#,Sharepoint,File Upload,我正在尝试使用SharePoint和C#将文件上载到文件夹 我用我的代码创建了文件夹,效果很好 这是我的文档类: [DataContractAttribute] public class Document { [DataMemberAttribute] public string Name { get; set; } [DataMemberAttribute] public byte[] Content { get; set; } [DataMembe

我正在尝试使用SharePoint和C#将文件上载到文件夹

我用我的代码创建了文件夹,效果很好

这是我的文档类:

[DataContractAttribute]
public class Document
{
    [DataMemberAttribute]
    public string Name { get; set; }

    [DataMemberAttribute]
    public byte[] Content { get; set; }

    [DataMemberAttribute]
    public bool ReplaceExisting { get; set; }

    [DataMemberAttribute]
    public string Folder { get; set; }

    [DataMemberAttribute]
    public Dictionary<string, object> Properties { get; set; }

    public Document()
    {
        Properties = new Dictionary<string, object>();
    }

    public Document(string name, byte[] content)
    {
        Name = name;
        ReplaceExisting = false;
        Content = content;
        Properties = new Dictionary<string, object>();
    }

    public Document(string name, byte[] content, bool replace)
    {
        Name = name; 
        Content = content;
        ReplaceExisting = replace;
        Properties = new Dictionary<string, object>();
    }
}
程序崩溃,我得到一个错误,上面写着:找不到文件

我做错了什么


谢谢你的帮助

以下是通过CSOM上传到SharePoint的工作示例:

using (ClientContext conext = new ClientContext(site.url))
{
    List projectFiles = projects.Web.Lists.GetByTitle("Project Files");
    context.Load(projectFiles.RootFolder, w => w.ServerRelativeUrl);                       
    context.ExecuteQuery();

    FileStream documentStream = System.IO.File.OpenRead(filePath);
    byte[] info = new byte[documentStream.Length];
    documentStream.Read(info, 0, (int)documentStream.Length);

    string fileURL = projectFiles.RootFolder.ServerRelativeUrl + "/Folder/FileName.ext";

    FileCreationInformation fileCreationInformation = new FileCreationInformation();
    fileCreationInformation.Overwrite = true;
    fileCreationInformation.Content = info;
    fileCreationInformation.Url = fileURL;
    Microsoft.SharePoint.Client.File uploadFile = projectFiles.RootFolder.Files.Add(fileCreationInformation);
    context.Load(uploadFile, w => w.MajorVersion, w => w.MinorVersion);
    context.ExecuteQuery();
}

在您的情况下,我将上载文件和ExecuteQuery(),然后添加元数据并执行第二个查询,确保在文件后添加context.Load.add()。你试图一次做太多的事情,所以只需使用这段代码上传文件,然后按照自己的方式完成其他需要。此外,文件添加不会为您创建文件夹,因此请确保您指定了有效的路径。

您是否尝试创建文件夹?我无法在MVC中执行此操作,因为它在我的工作场所。您将收到一个错误,因为您试图在上载文件之前签出该文件。因为它还不存在,所以它失败了。只需删除uploadFile.CheckOut();它会很好的工作。如果要替换现有文档,需要通过查询找到该文件,然后替换其byte[]内容(如果要使用CheckOut);它仍然会掉下来,说文件找不到。
try
{
    uploadFile.CheckOut();
    context.ExecuteQuery();
}
using (ClientContext conext = new ClientContext(site.url))
{
    List projectFiles = projects.Web.Lists.GetByTitle("Project Files");
    context.Load(projectFiles.RootFolder, w => w.ServerRelativeUrl);                       
    context.ExecuteQuery();

    FileStream documentStream = System.IO.File.OpenRead(filePath);
    byte[] info = new byte[documentStream.Length];
    documentStream.Read(info, 0, (int)documentStream.Length);

    string fileURL = projectFiles.RootFolder.ServerRelativeUrl + "/Folder/FileName.ext";

    FileCreationInformation fileCreationInformation = new FileCreationInformation();
    fileCreationInformation.Overwrite = true;
    fileCreationInformation.Content = info;
    fileCreationInformation.Url = fileURL;
    Microsoft.SharePoint.Client.File uploadFile = projectFiles.RootFolder.Files.Add(fileCreationInformation);
    context.Load(uploadFile, w => w.MajorVersion, w => w.MinorVersion);
    context.ExecuteQuery();
}