Ejemplo n.º 1
0
        public async Task <StoreFileInfo> UploadFile(UploadDataInfo uploadInfo)
        {
            try
            {
                string container = string.Empty;
                if (!string.IsNullOrEmpty(uploadInfo.Container))
                {
                    container = uploadInfo.Container.ToLower();
                }
                else
                {
                    this.Logger.LogError("Conainer name is required");
                    return(new StoreFileInfo(false));
                }

                if (uploadInfo.Stream == null ||
                    uploadInfo.Stream.Length == 0 ||
                    string.IsNullOrEmpty(uploadInfo.FileName))
                {
                    this.Logger.LogError("File is required");
                    return(new StoreFileInfo(false));
                }

                // Get a reference to a container named "sample-container" and then create it
                BlobContainerClient blobContainer = new BlobContainerClient(this.connection, container);
                await blobContainer.CreateIfNotExistsAsync();

                // Get a reference to a blob named "sample-file" in a container named "sample-container"
                BlobClient blob = blobContainer.GetBlobClient(
                    this.GenerateFileName(uploadInfo.DirectoryName, uploadInfo.FileName));

                // Upload local file
                using (MemoryStream stream = new MemoryStream())
                {
                    uploadInfo.Stream.CopyTo(stream);
                    stream.Position = 0;
                    Response <BlobContentInfo> response = await blob.UploadAsync(
                        stream,
                        //new BlobHttpHeaders
                        //{
                        //    ContentType = uploadInfo.FileInfo.ContentType,
                        //},
                        conditions : null);
                }

                return(new StoreFileInfo(blob.Uri.AbsoluteUri, blob.Name));
            }
            catch (Exception e)
            {
                this.Logger.LogError(e, "Can not upload file");
                return(new StoreFileInfo(false));
            }
        }
        public async Task <StoreFileInfo> UploadFile(UploadDataInfo uploadInfo)
        {
            try
            {
                if (uploadInfo.Stream == null ||
                    uploadInfo.Stream.Length == 0 ||
                    string.IsNullOrEmpty(uploadInfo.FileName))
                {
                    this.Logger.LogError("File is required");
                    return(new StoreFileInfo(false));
                }

                Account account = new Account(
                    this.cloudName,
                    this.apiKey,
                    this.apiSecret);

                Cloudinary cloudinary = new Cloudinary(account);

                var uploadParams = new ImageUploadParams();

                ImageUploadResult uploadResult;
                using (MemoryStream stream = new MemoryStream())
                {
                    uploadInfo.Stream.CopyTo(stream);
                    stream.Position   = 0;
                    uploadParams.File = new FileDescription(uploadInfo.FileName, stream);
                    if (!string.IsNullOrEmpty(uploadInfo.DirectoryName))
                    {
                        uploadParams.Folder = uploadInfo.DirectoryName;
                    }

                    uploadResult = await cloudinary.UploadAsync(uploadParams);
                }

                if (uploadResult.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    return(new StoreFileInfo(uploadResult.Uri.AbsoluteUri, uploadResult.PublicId));
                }
                else
                {
                    return(new StoreFileInfo(false));
                }
            }
            catch (Exception e)
            {
                this.Logger.LogError(e, e.Message);
                return(new StoreFileInfo(false));
            }
        }
Ejemplo n.º 3
0
        public async Task <StoreFileInfo> UploadFile(UploadDataInfo uploadInfo)
        {
            // Azure request only lowerCase container name
            string container = uploadInfo.Container.ToLower();

            // Get a reference to a share and then create it
            ShareClient share = new ShareClient(this.connection, container);

            share.CreateIfNotExists();

            // Get a reference to a directory and create it
            ShareDirectoryClient directory = share.GetDirectoryClient(uploadInfo.DirectoryName);
            await directory.CreateIfNotExistsAsync();

            // Get a reference to a file and upload it
            ShareFileClient          file       = directory.GetFileClient(this.GenerateFileName(uploadInfo.FileName));
            Response <ShareFileInfo> uploadFile = file.Create(uploadInfo.Stream.Length);

            file.UploadRange(
                new HttpRange(0, uploadInfo.Stream.Length),
                uploadInfo.Stream);

            return(new StoreFileInfo(string.Empty, uploadFile.Value.SmbProperties.FileId));
        }