C# 将文件上载到Blob Azure中

C# 将文件上载到Blob Azure中,c#,asp.net-mvc,azure-storage-blobs,C#,Asp.net Mvc,Azure Storage Blobs,我正在尝试从MVC应用程序将文件上载到blob Azure,我遵循教程,我被困在这段代码中: using (var fileStream = System.IO.File.OpenRead(@"path\myfile")) { blockBlob.UploadFromStream(fileStream); } 如何使@“path\myfile”动态??如何检索文件的真实路径以将其放入其中?我也接受上传到blob的任何其他建议:) 更新 根据建议,我在视图中添加了代码: @model R

我正在尝试从MVC应用程序将文件上载到blob Azure,我遵循教程,我被困在这段代码中:

using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
}
如何使
@“path\myfile”
动态??如何检索文件的真实路径以将其放入其中?我也接受上传到blob的任何其他建议:)

更新

根据建议,我在视图中添加了代码:

@model RiPSShared.Models.RiPSModels.AgencyNote

<h2>PostFile</h2>

<form action="@Url.Action("PostFile", "AgencyNotes", new { NoteId=Model.aut_id})" method = "post" enctype="multipart/form-data">
    <label for="file1"> File name:</label>
    <input type="file" name="file" id="file1" />
    <input type="submit" value="Submit" />    
</form>

HttpPostedFileBase
将提供您需要的信息。具体来说,
InputStream
属性将为您提供流

[HttpPost]
public ActionResult PostFile(HttpPostedFileBase file, int NoteId)  
{   
    // Your existing code for azure blob access
    CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
    // make sure to a null check on file parameter before accessing the InputStream

    using (var s = file.InputStream)
    {
       blockBlob.UploadFromStream(s);
    }
    // to do :Return something
}

此外,还可以在配置文件中设置连接字符串,并从中获取连接字符串,然后将文件存储到blob存储中,首先获取其容器名称

     //StorageConnectionString string specified in configuration file
       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container.
        //specified container name
        CloudBlobContainer container = blobClient.GetContainerReference("myBlobcontainer");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        container.SetPermissions(
        new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("myBlob");

        // Create or overwrite the "myblob" blob with contents from a local file.
      using (var fileStream = file.InputStream)
        {
            blockBlob.UploadFromStream(fileStream);
        }

我建议您在问题中加入相关信息(您的视图中有表单post,您的操作方法参数为HttpPostedFileBase type),以帮助将来的读者。我知道是因为我读了你上一个问题。
     //StorageConnectionString string specified in configuration file
       CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container.
        //specified container name
        CloudBlobContainer container = blobClient.GetContainerReference("myBlobcontainer");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        container.SetPermissions(
        new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

        CloudBlockBlob blockBlob = container.GetBlockBlobReference("myBlob");

        // Create or overwrite the "myblob" blob with contents from a local file.
      using (var fileStream = file.InputStream)
        {
            blockBlob.UploadFromStream(fileStream);
        }