Luciano Valinho
Luciano Valinho

Reputation: 59

Sensenet API: Upload Files to Folder Structure using Client

I have an requirement that consists to upload a document to a specific folder structure on sensenet.

To create the folder structure I'm using the Tools.EnsurePathAsync. After this I upload the file to the folder structure that I just created. The folder is creating great. But the file only in a few cases is uploaded.

Task.Run(() => Tools.EnsurePathAsync(pathDocType)).Wait();

Task.Run(() =>{
   var stream = new MemoryStream(byteContent);
   Content.UploadAsync(pathDocType, "test.doc", stream).WaitAndUnwrapException();
   stream.Dispose();
}).Wait();

Upvotes: 1

Views: 107

Answers (1)

maros
maros

Reputation: 86

We have an uplader application wich can create the non exists folders, a copied the relevant method:

 private  bool CreateFolderPath(string parentPath, string pathToCreate)
        {
            try
            {
                string[] folders = pathToCreate.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string folderName in folders)
                {
                    var fileTargetContentFolderTask =  Content.LoadAsync(parentPath+"/"+folderName);
                    fileTargetContentFolderTask.Wait();
                    if (fileTargetContentFolderTask.Result == null)
                    {
                        var folder = Content.CreateNew(parentPath, ConfigurationManager.AppSettings["FolderType"], folderName);
                        var task = folder.SaveAsync();
                        task.Wait();
                    }
                    parentPath += "/" + folderName;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

Maybe you can user this method creating subfolders for upload. It is possible that a particular type is not allowed under that folder. You can read the following link: http://wiki.sensenet.com/Allowed_Child_Types

The uploader on github: https://github.com/marosvolgyiz/SNClientLibraryUploader The relevant source file: https://github.com/marosvolgyiz/SNClientLibraryUploader/blob/master/CLUplader/ClientLibraryUploader.cs

Upvotes: 1

Related Questions