Exemplo n.º 1
0
        private void SaveFile()
        {
            // get target container
            var container        = PortalContext.Current.ContextNode as GenericContent;
            var targetFolderName = HttpContext.Current.Request["TargetFolder"];

            if (!string.IsNullOrEmpty(targetFolderName))
            {
                var containerPath = RepositoryPath.Combine(container.Path, targetFolderName);
                container = Node.LoadNode(containerPath) as GenericContent;

                // create target container if does not exist
                if (container == null)
                {
                    using (new SystemAccount())
                    {
                        container      = new Folder(PortalContext.Current.ContextNode);
                        container.Name = targetFolderName;
                        container.AllowedChildTypes = AllowedContentTypesList;
                        container.Save();
                    }
                }
            }

            var contentTypeName = UploadHelper.GetContentType(Upload.PostedFile.FileName, null) ?? "File";
            var allowed         = UploadHelper.CheckAllowedContentType(container as GenericContent, contentTypeName);

            if (allowed)
            {
                var binaryData = UploadHelper.CreateBinaryData(Upload.PostedFile);
                var fileName   = binaryData.FileName.ToString();

                var content = SenseNet.ContentRepository.Content.CreateNew(contentTypeName, container, fileName);
                content.ContentHandler.AllowIncrementalNaming = true;   // uploaded files of users go to the same folder. avoid collision, do not overwrite files of each other
                content["Name"] = fileName;
                content.Fields["Binary"].SetData(binaryData);
                content.Save();

                // display uploaded file in repeater
                if (_fileListElements == null)
                {
                    _fileListElements = new List <FileListElement>();
                }

                _fileListElements.Add(new FileListElement {
                    FileName = content.Name, Path = content.Path
                });
            }
            else
            {
                ErrorLabel.Text          = "This type cannot be uploaded!";
                ErrorPlaceHolder.Visible = true;
            }
        }