コード例 #1
0
        public async Task <ActionResult> UploadFile([FromForm] string fileName, string Path)
        {
            try
            {
                string uploadfile = HelperMethod.GetFileNamePath(fileName, Path);

                // only file which has permitted extension is allowed to upload
                if (!HelperMethod.IsPermittedFile(fileName))
                {
                    return(StatusCode(415, $"Only ( {string.Join(",", HelperMethod.PERMITTED_FILE_EXTENSION)} ) extension supported"));
                }

                BlobClient _blobClient = _blobContainerClient.GetBlobClient(fileName);

                // if file size to upload is greater than 5MB, discard and return
                long fileLength = new System.IO.FileInfo(uploadfile).Length;
                if ((fileLength / 1024) / 1024 > 5)
                {
                    return(StatusCode(413, "file size more than 5 MB is not allowed to upload"));
                }

                using var fileStream = System.IO.File.OpenRead(uploadfile);
                await _blobClient.UploadAsync(fileStream, true);

                fileStream.Close();

                // successfully created blob hence 201
                return(StatusCode(201, $"{fileName} file successfully uploaded"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }
コード例 #2
0
        public async Task <ActionResult> DownloadFile(string fileName, string Path)
        {
            try
            {
                string downloadfile = HelperMethod.GetFileNamePath(fileName, Path);

                var _blobClient = _blobContainerClient.GetBlobClient(fileName);

                // Download the blob's contents and save it to a file
                BlobDownloadInfo download = await _blobClient.DownloadAsync();

                using FileStream downloadFileStream = System.IO.File.OpenWrite(downloadfile);
                await download.Content.CopyToAsync(downloadFileStream);

                downloadFileStream.Close();

                return(StatusCode(200, $"{fileName} downloaded successfully at {Path}"));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }