我在我的项目中使用.NET Core MVC。所以我有一个 Document.cs
看起来像
public class Document{
public int DocumentId { get; set; }
public string DocumentName { get; set; }
public int DocumentTypeId { get; set; }
public DocumentType DocumentType { get; set; }
public string DocumentFilePath { get; set; } //which contains the File Name for my File Upload function.
}
我的文件上传方法如下所示:
[HttpPost]
public async Task<IActionResult> CreateDocument (DocumentModel model)
{
string uniqueFileName = null;
if(model.DocumentFilePath != null)
{
string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "StoredFiles");
uniqueFileName = model.DocumentFilePath.FileName;
string filePath = Path.Combine(uploadsFolder, uniqueFileName);
await model.DocumentPath.CopyToAsync(new FileStream(filePath, FileMode.Create));
}
var entity = new Document()
{
DocumentName = model.DocumentName,
DocumentTypeId = model.DocumentTypeId,
DocumentFilePath = uniqueFileName,
};
_documentService.Create(entity); // Simple '_context.Set<Document>().Add(entity);'
return RedirectToAction("CreateDocument");
}
如您所见,我还有一个来 self 的 View 的 DocumentModel.cs
,如下所示:
public class DocumentModel
{
public int DocumentId { get; set; }
public string DocumentName { get; set; }
public IFormFile DocumentFilePath { get; set; }
public int DocumentTypeId { get; set; }
public List<DocumentType> DocumentTypes { get; set;} //For my SelectList in my View.
}
好吧,所以这实际上的作用是,正如您可以简单理解的那样,将文件上传到我的“wwwroot”文件夹下的“StoredFiles”,并获取“文件名”并将其存储在我的数据库中。
所以我想开始使用 Azure Blob 存储。因为我不想将文件存储在我的项目文件夹中。我观看了一些有关 Azure 存储及其工作原理的视频,但我无法理解如何将 FileUpload 方法转换为 AzureStorage 方法。
在我看来,我应该尝试做这样的事情
[HttpPost]
public async Task<IActionResult> CreateDocument (DocumentModel model)
{
string uniqueFileName = null;
if(model.DocumentFilePath != null)
{
//I think in this part I have to upload to Azure, then I have to get a
//return value for my DocumentFilePath for my "entity" object below.
}
var entity = new Document()
{
DocumentName = model.DocumentName,
DocumentTypeId = model.DocumentTypeId,
DocumentFilePath = uniqueFileName,
};
_documentService.Create(entity); // Simple '_context.Set<Document>().Add(entity);'
return RedirectToAction("CreateDocument");
}
或者我可以使用单独的方法,第一个方法将上传到 azure ,然后第二个方法保存到我的数据库。
如果你能帮助我,我会很高兴。谢谢。
最佳答案
如果使用 blob 存储,请首先安装 NuGet 包:Microsoft.Azure.Storage.Blob
然后尝试以下代码:
string storageConnection = CloudConfigurationManager.GetSetting("BlobStorageConnectionString");
CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnection);
CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("appcontainer");
//create a container if it is not already exists
if (await cloudBlobContainer.CreateIfNotExistsAsync())
{
await cloudBlobContainer.SetPermissionsAsync(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
}
//get Blob reference
CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(model.DocumentFilePath.Name);
cloudBlockBlob.Properties.ContentType = model.DocumentFilePath.ContentType;
using (var stream = model.DocumentFilePath.OpenReadStream())
{
await cloudBlockBlob.UploadFromStreamAsync(stream);
}
存储中文件的 URL 将为 https://xxxxx.blob.core.windows.net/{ContainerName}/{FileName}
关于entity-framework - 如何使用 Azure 存储和 Entity Framework 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58271818/