0

我曾尝试使用下面的代码如何将文件上传到Asp.Net Core中的azure blob?

public async void UploadSync(IEnumerable<IFormFile> files, string path) 
     { 
      string MyPath = path.Replace("https://browsercontent.blob.core.windows.net/blob1/", ""); 

      try 
      { 
       foreach (var file in files) 
       { 
        var newBlob = container.GetBlockBlobReference(MyPath); 
        await newBlob.UploadFromFileAsync(@"C:\Users\joy\Downloads\" + file.FileName); 

       } 
      } 
      catch (Exception ex) 
      { throw ex;} 

     } 

其实我已经上传JPG文件,但它在“应用程序/ octact蒸汽式”上传的文件上传到我的天蓝色斑点。如何解决它?

而我的情况是,当上传文件时,Windows资源管理器将打开选择要上传的文件。因此,如果我们提供的路径为静态如下,

newBlob.UploadFromFileAsync(@ “C:\用户\快乐\下载\” + file.FileName);

它不适用于应用程序。如何更改此代码以从各个位置上传文件?

回答

2

尝试使用UploadFromStream,让我知道结果

// 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("mycontainer"); 

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

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

https://docs.microsoft.com/en-us/azure/storage/blobs/storage-dotnet-how-to-use-blobs

+0

您可以通过包括设置更新您的答案,围绕“应用/ octact蒸”内容类型考虑的OP关注blockblob.Properties.ContentType属性 – Oliver

+0

@sumanth,你的代码工作的很好。谢谢。 同样如何从Azure blob存储中下载文件?您可以共享代码块以从asp.net核心中的azure blob下载文件 – Joy

相关问题