C# 如何在Asp.NETCore中将文件上载到azure blob?

C# 如何在Asp.NETCore中将文件上载到azure blob?,c#,azure,azure-storage,azure-storage-blobs,azure-blob-storage,C#,Azure,Azure Storage,Azure Storage Blobs,Azure Blob Storage,我已尝试使用以下代码将文件上载到我的azure blob中 public async void UploadSync(IEnumerable<IFormFile> files, string path) { string MyPath = path.Replace("https://browsercontent.blob.core.windows.net/blob1/", ""); try

我已尝试使用以下代码将文件上载到我的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;}

        }
public async void UploadSync(IEnumerable文件,字符串路径)
{
字符串MyPath=path.Replace(“https://browsercontent.blob.core.windows.net/blob1/", "");
尝试
{
foreach(文件中的var文件)
{
var newBlob=container.GetBlockBlobReference(MyPath);
等待newBlob.UploadFromFileAsync(@“C:\Users\joy\Downloads\”+file.FileName);
}
}
捕获(例外情况除外)
{throw ex;}
}
实际上我已经上传了jpg文件,但它是以“application/octact steam”类型上传的。如何解决

我的场景是,在上载文件时,windows资源管理器将打开以选择要上载的文件。因此,如果我们提供如下静态路径

newBlob.UploadFromFileAsync(@“C:\Users\joy\Downloads\”+file.FileName


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

尝试使用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);
} 

尝试使用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);
} 

您可以通过包括设置blockblob.Properties.ContentType来更新您的答案,以考虑OPs对“application/octact steam”内容类型的担忧property@sumanth,您的代码工作得很好。谢谢类似地,我如何从azure blob存储中下载文件?您能否在asp.net Core中共享从azure blob下载文件的代码块?您可以更新您的答案,以考虑有关“应用程序/octact steam”的OPs问题内容类型,包括设置blockblob.Properties.ContentTypeproperty@sumanth,您的代码工作得很好。谢谢类似地,如何从azure blob存储中下载文件?请在asp.net core中共享用于从azure blob下载文件的代码块