5

Bir MVC5 internet uygulamasını kodluyorum ve bir dosyamı kendi dosya sistemimden bir Azure Blob'a yüklemek için bazı yardımlarda bulunmak istiyorum.Bir dosyayı MVC görünümünden Azure blob depolama alanına nasıl yüklerim?

public void UploadTestFile(string localFileName) 
{ 
    string containerName = "TestContainer"; 
    string blockBlogName = "Test.txt"; 
    AzureService azureService = new AzureService(); 
    azureService.UploadFileToBlobStorage(containerName, blockBlogName, localFileName); 
} 

Ben dan UploadTestFile() işlevi nasıl emin değilim: Burada

public void UploadFileToBlobStorage(string containerName, string blockBlogName, string fileName) 
{ 
    // Retrieve storage account from connection string. 
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
     CloudConfigurationManager.GetSetting("StorageConnectionString")); 

    // Create the blob client. 
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

    // Retrieve reference to a previously created container. 
    CloudBlobContainer container = blobClient.GetContainerReference(containerName); 

    // Create the container if it doesn't already exist. 
    container.CreateIfNotExists(); 

    container.SetPermissions(
     new BlobContainerPermissions 
     { 
      PublicAccess = 
       BlobContainerPublicAccessType.Blob 
     }); 

    // Retrieve reference to a blob named "myblob". 
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(blockBlogName); 

    // Create or overwrite the "myblob" blob with contents from a local file. 
    using (var fileStream = System.IO.File.OpenRead(fileName)) 
    { 
     blockBlob.UploadFromStream(fileStream); 
    } 
} 

bir test dosyası yüklemek için benim fonksiyonudur: Burada

benim Azure kodu yükleme fonksiyonudur MVC Kullanıcının karşıya yüklenecek bir dosyaya götürebileceği görünümü.

Ajax kullanmalı mıyım yoksa bir MVC görünümünden yöntemi çağırarak bir dosya yükleyebilir miyim? Bununla ilgili biraz yardım alabilir miyim? peşin

sayesinde bir MVC Görünümü'nden UploadTestFile() işlevini çağırmak için

cevap

9

bir yolu Html.BeginForm() yöntemi kullanmaktır. Aşağıda bir örnek dahil ediyorum:

@using (Html.BeginForm("UploadTestFile", "INSERT_YOUR_CONTROLLER_NAME_HERE", FormMethod.Post, new { enctype = "multipart/form-data" })) { 
    <span> 
     <input type="file" name="myFile" multiple /> <br> 
     <input type="submit" value="Upload" /> 
    </span> 

} 

Ayrıca, kod üzerinde önerilerin bir çift:

  1. UploadFileToBlobStorage(): konteyner varlığı için kod denetler ve her istekte ayar izinleri. Container.CreateIfNotExists() ve container.SetPermissions (…) mantığını, ilk dağıtımda yalnızca bir kez yürütülmesi gereken ayrı bir başlatma işlevine ayırmanızı öneririz.

  2. UploadFileToBlobStorage(): Kodun, çok parçalı form verilerinin değil VM dosya sisteminden localFileName dosyasını yüklemeyi denemesi gibi görünüyor. Bir yaklaşım, HttpFileCollectionBase sınıfını ve Controller.Request özelliğini kullanmak olacaktır. Aşağıdaki Örnek:

    public void UploadFileToBlobStorage(string containerName, string blockBlogName, HttpFileCollectionBase files) { 
        // ..... 
    
        // Use this: 
        blockBlob.UploadFromStream(files[0].InputStream); // uploading the first file: you can enumerate thru the files collection if you are uploading multiple files 
    
        // Instead of this: Create or overwrite the "myblob" blob with contents from a local file. 
        using (var fileStream = System.IO.File.OpenRead(fileName)) { 
         blockBlob.UploadFromStream(fileStream); 
        } 
    } 
    
    [HttpPost] 
    public void UploadTestFile() { 
        string containerName = "TestContainer"; 
        string blockBlogName = "Test.txt"; 
        AzureService azureService = new AzureService(); 
    
        // Notice the Request.Files instead of localFileName 
        azureService.UploadFileToBlobStorage(containerName, blockBlogName, Request.Files); 
    } 
    

senin uçta çalışmaya varsa bana bildirin.

İlgili konular