/// <summary> /// 檔案上傳儲存於本地端 /// </summary> /// <param name="file">上傳之檔案</param> /// <param name="folder">欲存放資料夾名稱</param> /// <returns>T.</returns> /// <exception cref="System.NullReferenceException">檔案為空或是沒有任何檔案長度</exception> protected internal static CiFile SaveLocal(HttpPostedFileBase file, string folder = "Temps", string fileName = "") { if (IsNullOrEmpty(file)) { throw new NullReferenceException("檔案為空或是沒有任何檔案長度"); } var ext = Path.GetExtension(file.FileName); if (string.IsNullOrWhiteSpace(fileName)) { fileName = GetNewFileName(); } CiFile model = new CiFile { Extension = ext, NewName = fileName, OriName = Path.GetFileNameWithoutExtension(file.FileName), Folder = folder, VirtualPath = Path.Combine(RootPath, folder) }; model.FullPath = HttpContext.Current.Server.MapPath(Path.Combine(model.VirtualPath, model.NewName + model.Extension)); // 若檔名重複則自動重新取得編號 while (File.Exists(model.FullPath)) { model.NewName = GetNewFileName(); model.FullPath = HttpContext.Current.Server.MapPath(Path.Combine(model.VirtualPath, model.NewName + model.Extension)); } // 檢查資料夾是否存在,若不存在則自動建立 if (!Directory.Exists(Path.GetDirectoryName(model.FullPath))) { Directory.CreateDirectory(Path.GetDirectoryName(model.FullPath)); } // 存檔 file.SaveAs(model.FullPath); model.VirtualPath = Path.Combine(model.VirtualPath, model.NewName + model.Extension); return model; }
protected internal async static Task<CiFile> SaveAzureStorageAsync(HttpPostedFileBase file, string containerName, string fileName, string connectionString) { if (IsNullOrEmpty(file)) { throw new ArgumentNullException(nameof(file), "file is null or empty"); } var ext = Path.GetExtension(file.FileName); if (string.IsNullOrWhiteSpace(fileName)) { fileName = GetNewFileName(); } var ciFile = new CiFile() { Extension = ext, Folder = containerName, NewName = fileName, OriName = Path.GetFileNameWithoutExtension(file.FileName), StorageType = StorageType.Azure }; // initlize storageaccount by various setting if (string.IsNullOrWhiteSpace(connectionString)) { if (CiConfig.Global.CiFile.AzureStorage.ConnectionString != null && !string.IsNullOrWhiteSpace(CiConfig.Global.CiFile.AzureStorage.ConnectionString.ToString())) { connectionString = CiConfig.Global.CiFile.AzureStorage.ConnectionString.ToString(); } else { throw new ArgumentNullException(nameof(CiConfig.Global.CiFile.AzureStorage.ConnectionString), "AzureStorage ConnectionString is empty."); } } CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve a reference to a container, container name must be loweer case. CloudBlobContainer container = blobClient.GetContainerReference(containerName); container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Off }); // Create the container if it doesn't already exist. container.CreateIfNotExists(); // Retrieve reference to a blob named "myblob", aka filename. CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName + ext); await blockBlob.UploadFromStreamAsync(file.InputStream); ciFile.FullPath = blockBlob.StorageUri.ToString(); ciFile.VirtualPath = blockBlob.Uri.ToString(); return ciFile; }