public async Task<UploadDto> UploadFile(string fileName, string contentType, Stream stream)
        {
            // CloudConfigurationManager.
            var account = CloudStorageAccount.Parse(settings.AzureblobStorageConnectionString);
            var client = account.CreateCloudBlobClient();
            var container = client.GetContainerReference(UploadContainer);

            // Randomize the filename everytime so we don't overwrite files
            string randomFile = Path.GetFileNameWithoutExtension(fileName) +
                                "_" +
                                Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(fileName);

            await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Blob, new BlobRequestOptions(), new OperationContext());



            CloudBlockBlob blockBlob = container.GetBlockBlobReference(randomFile);
            blockBlob.Properties.ContentType = contentType;

            await blockBlob.UploadFromStreamAsync(stream);

            //  await Task.Factory.FromAsync((cb, state) => blockBlob.UploadFromStreamAsync(stream, cb, state), ar => blockBlob.EndUploadFromStream(ar), null);

            var result = new UploadDto
            {
                FileName = fileName,
                Url = blockBlob.Uri.ToString(),
            };

            return result;
        }
        public async Task<UploadDto> UploadFile(string fileName, string contentType, Stream stream)
        {
            var uploadDirectoryPath = Path.Combine(hostingEnvironment.WebRootPath, settings.LocalFileSystemStoragePath);

            string randomFile = Path.GetFileNameWithoutExtension(fileName) +
                                "_" +
                                Guid.NewGuid().ToString().Substring(0, 4) + Path.GetExtension(fileName);


            if (!Directory.Exists(uploadDirectoryPath))
            {
                Directory.CreateDirectory(uploadDirectoryPath);
            }


            var targetFile = Path.GetFullPath(Path.Combine(uploadDirectoryPath, randomFile));

            using (var destinationStream = File.Create(targetFile))
            {
                await stream.CopyToAsync(destinationStream);
            }


            var result = new UploadDto
            {
                FileExtn = Path.GetExtension(fileName),
                FileName =  fileName,
                Url = GetFullUrl(settings.LocalFileSystemStorageUriPrefix, randomFile)
            };

            return result;
        }
 public async Task<int> SaveUpload(UploadDto uploadDto)
 {
     var q =
       @"INSERT INTO [dbo].[Upload](Filename,Type,Url,CreatedDate,CreatedById,ParentId) VALUES(@fileName,@Type,@Url,@CreatedDate,@CreatedById,@ParentId);SELECT CAST(SCOPE_IDENTITY() as int)";
     using (var con = new SqlConnection(ConnectionString))
     {
         con.Open();
         var ss = await con.QueryAsync<int>(q,uploadDto);
         return ss.Single();
     }
 }
Пример #4
0
 public async Task<int> SaveUpload(UploadDto uploadDto)
 {
     uploadDto.CreatedDate = DateTime.UtcNow;
     return await uploadRepository.SaveUpload(uploadDto);
 }