-1

i am working on a website where you could upload a image onto azure storage as a blob and also to download the blob, however when i download the image it gives me a "this file format cannot be opened" when opening the pic. i was wondering where could i display the fileStream?

heres my 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>

This is the javascript method ive written in order to download the blob however i've been trying to find out where the problem

In my controller class i have:

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

and in my storage class:

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;
    }
}
live2
  • 2,620
  • 2
  • 25
  • 35
SomaliLad
  • 1
  • 3

2 Answers2

0

You are using your file name as your extension in your Action;

extension = @item.FileNameWithoutExt

It could explain why get the 404, as the blob probably don't exist with this extension.

Benjamin Ø.
  • 115
  • 1
  • 8
0

We need to use the @Url.Action as following:@Url.Action("actionName","controlName",routeValues).

Gives a HTTP404 Error (Azure Storage/Visual Studio)

It indicates that can't route that path. So you could add the controller named DownloadBlob and add the method Download. We need to make sure that it has the same paramter name are in the 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());
}

The following is the test result on my side.

enter image description here

Tom Sun - MSFT
  • 22,436
  • 3
  • 23
  • 40