c# - 将文件上传到 Blob Azure

标签 c# asp.net-mvc azure-blob-storage

我正在尝试从 MVC 应用程序将文件上传到 blob Azure,我正在关注 this教程,我被困在这个片段中:

using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blockBlob.UploadFromStream(fileStream);
}

如何使 @"path\myfile" 动态化??如何检索我的文件的真实路径以将其放在那里?我也接受任何其他上传到 blob 的建议 :)

更新

按照建议,我在 View 中添加了代码:

@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>

以及完整的 Action Controller :

 public ActionResult PostFile(HttpPostedFileBase file, int NoteId)
 {
 CloudBlockBlob blockBlob = container.GetBlockBlobReference(path);
 using (Stream fileStream = file.InputStream)
            {
                blockBlob.UploadFromStream(fileStream);
            }
 return RedirectToAction("Index");
}

最佳答案

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
}

关于c# - 将文件上传到 Blob Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40364856/

相关文章:

用于托管图像/媒体的 Azure Blob 存储 - 使用 blob URL 获取(无需中间 Controller )

C# 在哪里放置检索 GUI 数据的代码?

asp.net-mvc - ASP.NET MVC 和 LLBLGEN

c# - 在 ASP.NET MVC 中使用 SignalR Hub 和 Controller 类的正确方法

c# - Azure blob 上传在 .Net Core 3.0 应用程序中调用 .Upload() 时挂起

azure-storage - 检查 Azure 的共享访问签名有效性/过期

c# - 制作用户控制脉冲

c# - 非托管导出/Delphi/.NET 4/Robert Giesecke

c# - 如何在从 ASP.Net 应用程序作为电子邮件发送的文本消息中包含换行符?

c# - 带有 Crystal Report 的 .Net 应用程序无法在共享主机中运行