簡體   English   中英

如何使用 Azure 存儲和實體框架上傳文件

[英]How to Upload Files using Azure Storage and Entity Framework

我在我的項目中使用 .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");
}

如您所見,我還有一個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 Storage。 因為我不想將文件存儲在我的項目文件夾中。 我觀看了一些有關 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 package: 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}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM