File upload 从httppostedfile将二进制文件保存到blob

File upload 从httppostedfile将二进制文件保存到blob,file-upload,azure,binary,blob,File Upload,Azure,Binary,Blob,有人能提供代码以二进制形式将上传的文件保存到azure blob吗?我目前使用的文本保存在大文件上速度非常慢,逐行读取/保存到blob Private Function ReadFile(ByVal file As HttpPostedFile) As String Dim result As String = "" Dim objReader As New System.IO.StreamReader(file.InputStream) Do W

有人能提供代码以二进制形式将上传的文件保存到azure blob吗?我目前使用的文本保存在大文件上速度非常慢,逐行读取/保存到blob

Private Function ReadFile(ByVal file As HttpPostedFile) As String
        Dim result As String = ""
        Dim objReader As New System.IO.StreamReader(file.InputStream)
        Do While objReader.Peek() <> -1
            result = result & objReader.ReadLine() & vbNewLine
        Loop
        Return result
    End Function
Private函数ReadFile(ByVal文件作为HttpPostedFile)作为字符串
将结果变暗为字符串=“”
Dim objReader作为新System.IO.StreamReader(file.InputStream)
Do While objReader.Peek()-1
result=result&objReader.ReadLine()&vbNewLine
环
返回结果
端函数

谢谢

此代码片段基于将照片推送到blob存储中的生产应用程序。这种方法直接从HttpPostedFile中提取流,并将其直接交给客户机库以存储到blob中。您应该根据您的应用程序更改一些内容:

  • blobName可能需要修改
  • 获取blob客户机之前的connectionstring应该被隔离到helper类中
  • 类似地,您可能需要一个基于业务逻辑的blob容器助手
  • 您可能不希望容器完全可公开访问。这只是为了告诉你如果你喜欢的话该怎么做
//假设HttpPostedFile位于名为postedFile的变量中
var contentType=postedFile.contentType;
var streamContents=postedFile.InputStream;
var blobName=postedFile.FileName
var connectionString=CloudConfigurationManager.GetSetting(“YOURSTORAGEACCOUNT\u connectionString”);
var storageAccount=CloudStorageAccount.Parse(connectionString);
var blobClient=storageAccount.CreateCloudBlobClient();
var container=blobClient.GetContainerReference(“YOURCONTAINERNAME”);
container.CreateIfNotExist();
SetPermissions(新的BlobContainerPermissions{PublicAccess=BlobContainerPublicAccessType.Blob});
var blob=container.GetBlobReference(blobName);
blob.Properties.ContentType=ContentType;
blob.UploadFromStream(streamContents);
6年后,答案似乎与WindowsAzure.Storage v9.3.2不兼容

对我来说,这很有效:

IFormFile postedFile = null;
var contentType = postedFile.ContentType;
var blobName = postedFile.FileName;

var connectionString = "YOURSTORAGEACCOUNT_CONNECTIONSTRING";
var storageAccount = CloudStorageAccount.Parse(connectionString);
var blobClient = storageAccount.CreateCloudBlobClient();

var container = blobClient.GetContainerReference("YOURCONTAINERNAME");
await container.CreateIfNotExistsAsync();

var blob = container.GetBlockBlobReference(blobName);
blob.Properties.ContentType = contentType;
using (var streamContents = postedFile.OpenReadStream())
{
    await blob.UploadFromStreamAsync(streamContents);
}