Azure使用url下载blob

时间:2016-05-22 07:34:13

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

我在Visual Studio上创建了一个Azure Cloud Service项目。我已经能够在azure blob存储中上传我的文件了。我需要的是,当点击一个按钮时,它会返回上传的blob的URL,例如http://127.0.0.1:10000/devstoreaccount1/conversions/filename.extension

在我看来,我做了以下事情:

<div class="table">
    <table class="table table-responsive">
        <thead>
            <tr>
                <th>File Name</th>
                <th>ConversionID</th>
                <th>Actual Format</th>
                <th>Status</th>
                <th>Download</th>
            </tr>
        </thead>
        @foreach (var item in Model)
            {
            <tr>
                <td>@item.FileName</td>
                <td>@item.ConversionID</td>
                <td>@item.Format</td>
                <td>@item.Status</td>
                <td>
                    <a href="/Download.aspx?conversionID=@item.ConversionID">
                        <span class="DownloadListItem">
                            <b>Download<b>
                        </span>
                    </a>
            </tr>
        }
    </table>
</div>

我的Download.aspx.cs:

    private ApplicationDbContext db = new ApplicationDbContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        string rawId = Request.QueryString["ConversionID"];
        int converionID;

        if (!String.IsNullOrEmpty(rawId) && int.TryParse(rawId, out converionID))
        {
          var v = db.Conversions.Where(a => a.ConversionID.Equals(converionID));  

            if (v != null)
            {
                //return ConversionURL
            }   
        }

        else
        {
            Debug.Fail("ERROR : We should never get to AddToCart.aspx without a ConversionID.");
            throw new Exception("ERROR : It is illegal to load AddToCart.aspx without setting a ConversionID.");
        }
    }

如何在代码中返回ConversionURL?

修改

当我上传文件时,我将文件及其内容上传到Azure Blob存储中,但我还在SQL Server数据库上存储了上传文件的ConversionID和ConversionURL。

1 个答案:

答案 0 :(得分:2)

上传blob后,您可以访问Uri property of cloud blob。上传到数据库后保存,并在获得ConversionId后返回。

        blob.UploadFromFile(...);
        var blobUrl = blob.Uri;