gpt4 book ai didi

c# - 如何将文件上传到 Asp.Net Core 中的 azure blob?

转载 作者:行者123 更新时间:2023-12-03 05:04:36 26 4
gpt4 key购买 nike

我尝试使用以下代码将文件上传到我的 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 文件,但它以“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);
}

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

关于c# - 如何将文件上传到 Asp.Net Core 中的 azure blob?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47487351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com