繁体   English   中英

如何在Asp.Net Core中将文件上传到Azure Blob?

[英]How to upload a file in to azure blob in 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文件,但它以“应用程序/八位字节码”类型上传。 怎么解决呢?

我的情况是上传文件时,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);
} 

https://docs.microsoft.com/zh-cn/azure/storage/blobs/storage-dotnet-how-to-use-blobs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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