5

MVC5 인터넷 응용 프로그램을 코딩 중이며 내 파일 시스템에서 Azure Blob로 파일을 업로드하는 데 도움이 필요합니다.MVC보기에서 Azure blob 저장소에 파일을 업로드하는 방법

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

난에서 UploadTestFile() 함수를 호출하는 방법을 잘 모르겠습니다 : 여기

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); 
    } 
} 

테스트 파일을 업로드 내 기능입니다 : 여기

내 푸른 업로드 코드 기능입니다 MVC 사용자가 업로드 할 파일을 찾을 수있는 위치를 표시합니다.

Ajax를 사용해야합니까, 아니면 단순히 MVC 뷰에서 메서드를 호출하여 파일을 업로드 할 수 있습니까? 이걸 좀 도와 주시겠습니까? 사전에

감사 MVC보기에서 UploadTestFile() 함수를 호출하는

답변

9

한 가지 방법은 Html.BeginForm() 메소드를 사용하는 것입니다. 나는 아래 예제를 포함하고있다 :

@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> 

} 

또한, 코드에 몇 가지 제안 :

  1. UploadFileToBlobStorage() : 컨테이너의 존재에 대한 코드를 확인하고 모든 요청에 ​​대한 권한 설정. container.CreateIfNotExists() 및 container.SetPermissions (...) 로직을 첫 번째 배포시 한 번만 실행해야하는 별도의 초기화 함수로 분리하는 것이 좋습니다.

  2. UploadFileToBlobStorage() : 코드가 다중 파트 양식 데이터가 아닌 VM 파일 시스템에서 localFileName을 업로드하려고 시도하는 것처럼 보입니다. 한 가지 방법은 HttpFileCollectionBase 클래스와 Controller.Request 속성을 사용하는 것입니다. 아래 예 :

    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); 
    } 
    

그 최종에 작동하는지 알려 주시기 바랍니다.

관련 문제