Yogs
Yogs

Reputation: 1

Sensenet: upload files through sensenet client API for version 6.5 is not working properly

I have installed SenseNet version 6.5 (Code from codeplex). Wanted to upload the files in content repositry using Sensenet Client API, unfortunately it is not working with bulk upload.

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

Upvotes: 0

Views: 105

Answers (2)

maros
maros

Reputation: 86

We created a small application with SN ClientLibrary. I think, you can use this application/information/code.

This application can upload entire folders via Client Libray. Please check it out my Github repository: https://github.com/marosvolgyiz/SNClientLibraryUploader

There is relevant upload method:

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

I hope my answer is helpful to you.

Br, maros

Upvotes: 1

Mikl&#243;s T&#243;th
Mikl&#243;s T&#243;th

Reputation: 1511

There are two problems with the code above:

  1. you have to 'await' for async methods. Currently you start the task with the UploadAsync method, but you do not wait for it to finish, which casuses problems, because the file stream closes immediately after starting the upload task. Please upload files in an async way (of course you'll have to make your caller method async too, but that is the point of using an async api):
await Content.UploadAsync(...)

You may also consider using the Importer class in the client, it is able to import full directory structures.

  1. You are trying to upload into an (apps) folder, which is not a correct target, that was designed to contain applications (mostly pages). It would be better if you uploaded into a document library in a workspace, for example:

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

Upvotes: 2

Related Questions