CloudBlob.DownloadToStream返回null [英] CloudBlob.DownloadToStream returns null

查看:220
本文介绍了CloudBlob.DownloadToStream返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过流从cloudBlob下载文件。我指的是这篇文章 CloudBlob

下面是code下载BLOB

 公共流DownloadBlobAsStream(CloudStorageAccount账户,串blobUri)
{
    流纪念品=新的MemoryStream();
    CloudBlobClient blobclient = account.CreateCloudBlobClient();
    CloudBlockBlob BLOB = blobclient.GetBlockBlobReference(blobUri);    如果(BLOB!= NULL)
        blob.DownloadToStream(MEM);    返回MEM;
}

而code将其转换成字节数组

 公共静态的byte []的readFully(流输入)
    {
        字节[]缓冲区=新的字节[16 * 1024];
        使用(MemoryStream的毫秒=新的MemoryStream())
        {
            INT读;
            而((读取= input.Read(缓冲液,0,buffer.Length))大于0)
            {
                ms.Write(缓冲,0,读);
            }
            返回ms.ToArray();
        }
    }

不过,我总是得到null值。下面是该流文件的内容。

有什么不对呢?请帮助。

修改

设置位置为0,在的readFully 方法是不允许的,所以我把它放在 DownloadBlobAsStream

这应该现在的工作:

 公共流DownloadBlobAsStream(CloudStorageAccount账户,串blobUri)
{
    流纪念品=新的MemoryStream();
    CloudBlobClient blobclient = account.CreateCloudBlobClient();
    CloudBlockBlob BLOB = blobclient.GetBlockBlobReference(blobUri);    如果(BLOB!= NULL)
        blob.DownloadToStream(MEM);
    mem.Position = 0;
    返回MEM;
}


解决方案

您的问题是,你的输入流指针设置结束蒸汽(见截屏,长度和位置都显示相同的值),这就是为什么当你读它你总是空。您需要使用设置为输入流的指针为0 Stream.Position = 0 如下:

 公共静态的byte []的readFully(流输入)
{
    字节[]缓冲区=新的字节[16 * 1024];    input.Position = 0; //加入这一行设定输入流位置为0    使用(MemoryStream的毫秒=新的MemoryStream())
    {
        INT读;
        而((读取= input.Read(缓冲液,0,buffer.Length))大于0)
        {
            ms.Write(缓冲,0,读);
        }
        返回ms.ToArray();
    }
}

I'm trying to download a file from cloudBlob via stream. I refer to this article CloudBlob

Here is the code to download the blob

public Stream DownloadBlobAsStream(CloudStorageAccount account, string blobUri)
{
    Stream mem = new MemoryStream();
    CloudBlobClient blobclient = account.CreateCloudBlobClient();
    CloudBlockBlob blob = blobclient.GetBlockBlobReference(blobUri);

    if (blob != null)
        blob.DownloadToStream(mem);

    return mem;
}  

And the code to convert it into byte array

    public static byte[] ReadFully(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }  

But I always get null value. Below is the content of the streamed file.

What is wrong with this? Please help.

EDIT

Setting the Position to 0 inside ReadFully method is not allowed, so I put it inside DownloadBlobAsStream

This should work now:

public Stream DownloadBlobAsStream(CloudStorageAccount account, string blobUri)
{
    Stream mem = new MemoryStream();
    CloudBlobClient blobclient = account.CreateCloudBlobClient();
    CloudBlockBlob blob = blobclient.GetBlockBlobReference(blobUri);

    if (blob != null)
        blob.DownloadToStream(mem);
    mem.Position = 0;   
    return mem;
} 

解决方案

Your problem is that your input stream pointer is set to end of the steam (See the screen shot, Length and Position both shows same value) that's why when you read it you always get null. You would need to set to input stream pointer to 0 using Stream.Position = 0 as below:

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16 * 1024];

    input.Position = 0; // Add this line to set the input stream position to 0

    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
} 

这篇关于CloudBlob.DownloadToStream返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆