0

我已经安装了 SenseNet 6.5 版(来自 codeplex 的代码)。想要使用 Sensenet 客户端 API 上传内容存储库中的文件,不幸的是它不适用于批量上传。

    string [] fileEntries = Directory.GetFiles(@"C:\Users\conyna\Downloads\Chirag");

             foreach (string fileName in fileEntries)
             {
                 using (Stream fs = File.OpenRead(fileName))
                 {
                     string fn = Path.GetFileName(fileName);

                     Task<SenseNet.Client.Content> x =   SenseNet.Client.Content.UploadAsync("/Root/Sites/Default_Site/workspaces/(apps)/DocumentLibrary", fn, fs);
                 }
             }
4

2 回答 2

2

上面的代码有两个问题:

  1. 您必须“等待”异步方法。当前您使用UploadAsync方法启动任务,但您不等待它完成,这会导致问题,因为文件流在启动上传任务后立即关闭。请以异步方式上传文件(当然,您也必须使调用方方法异步,但这是使用异步 api 的重点):
await Content.UploadAsync(...)

您也可以考虑在客户端使用Importer 类,它能够导入完整的目录结构。

  1. 您正在尝试上传到 (apps) 文件夹,该文件夹不是正确的目标,旨在包含应用程序(主要是页面)。如果您上传到工作区中的文档库会更好,例如:

/Root/Sites/Default_Site/workspaces/Document/SampleWorkspace/DocumentLibrary

于 2017-05-02T12:50:34.973 回答
1

我们使用 SN ClientLibrary 创建了一个小型应用程序。我认为,您可以使用此应用程序/信息/代码。

此应用程序可以通过 Client Libray 上传整个文件夹。请查看我的 Github 存储库:https ://github.com/marosvolgyiz/SNClientLibraryUploader

有相关的上传方法:

public async Task Upload()
        {
            try
            {
                Console.WriteLine("Initilization...");
                ClientContext.Initialize(new[] { sctx });

                Console.WriteLine("Upload Started");
                //Is Parent exists
                var content = await Content.LoadAsync(Target);
                if (content != null)
                {
                    //Uploading files
                    var tasks = new List<Task>();
                    foreach (var file in Files)
                    {
                        string fileTargetFolder = Target + file.DirectoryName.Replace(Source, "").Replace(BaseDirectory, "").Replace("\\", "/");
                        var fileTargetContentFolder = await Content.LoadAsync(fileTargetFolder);
                        if (fileTargetContentFolder == null)
                        {
                            if (CreateFolderPath(Target, file.DirectoryName.Replace(Source, "")))
                            {
                                fileTargetContentFolder = await Content.LoadAsync(fileTargetFolder);
                                Console.WriteLine("#Upload file: " + file.FullName);
                                tasks.Add(Content.UploadAsync(fileTargetContentFolder.Id, file.Name, file.OpenRead()));
                                LoggerClass.LogToCSV("File uploaded", file.Name);
                            }
                            else
                            {
                                LoggerClass.LogToCSV("File target folder does not exist or you do not have enough permission to see! File can not be uploaded. ", file.Name);
                            }
                        }
                        else
                        {
                            Console.WriteLine("#Upload file: " + file.FullName);
                            tasks.Add(Content.UploadAsync(fileTargetContentFolder.Id, file.Name, file.OpenRead()));
                            LoggerClass.LogToCSV("File uploaded", file.Name);
                        }
                    }
                    await Task.WhenAll(tasks);
                }
                else
                {
                    Console.WriteLine("Target does not exist or you do not have enough permission to see!");
                    LoggerClass.LogToCSV("Target does not exist or you do not have enough permission to see!");
                }
                Console.WriteLine("Upload finished.");
            }
            catch (Exception ex)
            {
                LoggerClass.LogToCSV(ex.Message);
            }
        }

希望我的回答对你有帮助。

兄弟,马罗斯

于 2017-12-15T10:58:01.370 回答