简体   繁体   中英

How to open the file on browser instead of downloading in local that is stored in Azure Data lake Gen 2

Currently, I am generating SAS token from Azure file share and trying to access the file. But when I hit that url on browser, file automatically getting download instead of opening directly on browser.

My goal is to generate the SAS token from Azure file share and open that file(.pdf, .txt, .mkv, .docx) directly on browser without downloading using that SAS token.

• When you are trying to access the images on a file share using the SAS token URI, they are bound to be downloaded since they are supposed to be accessed by the SAS token and not display the data in the browser itself. As if the images or any data stored on the file share were to be opened and displayed on the browser itself, it would have been a breach of privacy and the data encryption also with which it was secured . Thus, considering the security and accessibility of files stored in Azure storage, the files are downloaded when accessed using SAS token.

But you surely can open the files/images in the browser as in a preview state before printing by creating a web app service that uploads images/files in a blob storage account and displays it upon request by using the SAS URI token in the backend. For this, you will have to create an ASP .Net MVC application and configure it to use SAS URI instead of the account key to retrieve images from the blob storage account. Please find the source code of the web app below: -

     //ViewModel
   public class ViewModel
  {
   public string FileUrl { get; set; }
    }


    public ActionResult Index()

     {
         var readPolicy = new SharedAccessBlobPolicy()
         {
             Permissions = SharedAccessBlobPermissions.Read,
             SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(5)
         };
            
         // Retrieve storage account from connection string.
         string conn = "DefaultEndpointsProtocol=https;AccountName=straccountname;AccountKey=key==;EndpointSuffix=core.windows.net";
         Microsoft.WindowsAzure.Storage.CloudStorageAccount storageAccount = CloudStorageAccount.Parse(conn);
         // Create the blob client.
         CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
         // Retrieve reference to a previously created container.
         CloudBlobContainer container = blobClient.GetContainerReference("test");
         // Retrieve reference to a blob ie "20200809_125724.jpg".
         CloudBlockBlob blockBlob = container.GetBlockBlobReference("20200809_125724.jpg");
         //------
         var newUri = new Uri(blockBlob.Uri.AbsoluteUri + blockBlob.GetSharedAccessSignature(readPolicy));
         var viewModel = new ViewModel()
         {
             FileUrl = newUri.ToString()
         };
         return View("Index", viewModel);
         // return View();
     } 

Further, for more information and details, I would suggest you to please refer the link below: -

https://docs.microsoft.com/en-us/answers/questions/252303/sas-url-to-display-in-browser-rather-than-download.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM