出现HTTP404错误(Azure存储/ Visual Studio)

时间:2018-03-27 17:09:14

标签: c# azure azure-storage azure-storage-blobs filestream

我正在一个网站上工作,你可以将图像作为blob上传到azure存储,也可以下载blob,但是当我下载图像时它会给我一个"这个文件格式无法打开&#34 ;打开图片时。我想知道我在哪里可以显示fileStream?

继承人我的javascript:

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="container">

@Html.ActionLink("Upload to Azure Blob", "UploadBlob", new { Controller = "blob" }, new { @class = "btn btn-link" })

<div class=" table table-striped table-responsive">

    <table id=" tablex">
        <thead>
            <tr>
                <th>Container</th>
                <th>Actual FileName</th>
                <th> Uri </th>

            </tr>
        </thead>

        <tbody>
            @if(Model != null)
            {
                foreach (var item in Model)
                {

            <tr id="row_@item.PrimaryUri">
                <td>@item.BlockContainerName</td>
                <td>@item.ActualFileName</td>

                <td>
                    <a herf=@item.PrimaryUri>@item.PrimaryUri </a>
                </td>
                @*<td>@Html.ActionLink("Remove", "DeletedBlob", new { controller = "blob", file = @item.FileNameWithoutExt, extension = @item.FileNameWithoutExt })</td> *@

                <td><a href='@Url.Action("Download", "DownloadBlob", new { file = @item.FileNameWithoutExt, extension = @item.FileNameWithoutExt })'>Download</a></td>

                <td>
                    <input type="submit" href="#" class="btn btn-link" id="btndel" value="Remove" data-id="@item.ActualFileName" />
                </td>

            </tr>

                }

            }

        </tbody>



    </table>
</div>
</div>

这是我为了下载blob而编写的javascript方法,但是我一直试图找出问题所在

在我的控制器课程中,我有:

public async Task<ActionResult> DownloadBlob(string file, string extension)
{
    string downloadPath = await repo.DownloadBlobAsync(file, extension);
    return Json(downloadPath);
}

在我的存储类中:

public async Task<string> DownloadBlobAsync (string file, string fileExtension)
{
    _cloudBlobContainerx = _cloudBlobClientx.GetContainerReference(containerNamex);
    CloudBlockBlob blockBlob = _cloudBlobContainerx.GetBlockBlobReference(file + "." + fileExtension);

    var path = downloadPath + file + "." + fileExtension;
    using (var fileStream = System.IO.File.OpenWrite(path))
    {
        //fileStream.Position = 1;
        //fileStream.Seek(0, SeekOrigin.Begin);

        await blockBlob.DownloadToStreamAsync(fileStream);

        return path;
    }
}

2 个答案:

答案 0 :(得分:0)

您正在使用文件名作为行动中的扩展程序;

extension = @item.FileNameWithoutExt

它可以解释为什么得到404,因为这个扩展可能不存在blob。

答案 1 :(得分:0)

我们需要使用@ Url.Action,如下所示:@Url.Action("actionName","controlName",routeValues)

  

出现HTTP404错误(Azure存储/ Visual Studio)

表示无法路由该路径。因此,您可以添加名为DownloadBlob的控制器,并添加方法下载。我们需要确保它在routeValues中具有相同的参数名称。

public void Download(string file, string extension)
{
    var container = cloudBlobClient.GetContainerReference("test");
    container.CreateIfNotExists();
    var blockBlob = container.GetBlockBlobReference(file + "." + fileExtension);
    blockBlob.FetchAttributes();
    var contentType = blockBlob.Properties.ContentType;   
    MemoryStream memStream = new MemoryStream();
    blockBlob.DownloadToStream(memStream);
    var response = HttpContext.Response;
    response.ContentType = contentType;
    response.AddHeader("Content-Disposition", "Attachment; filename=" + file + "." + fileExtension);
    response.AddHeader("Content-Length", blockBlob.Properties.Length.ToString());
    response.BinaryWrite(memStream.ToArray());
}

以下是我方的测试结果。

enter image description here