无法将图像上传到Azure blob

时间:2015-03-13 11:26:23

标签: c# asp.net-mvc image file-upload azure-storage-blobs

我正在尝试将图像上传到azure blob,但是当我在blob中看到上传的图像时,其大小为0bytes。

正如我所知,它正在发生,因为 InputStream (HttpPostedFileBase.InputStream)的位置属性变得等于长度 InputStream的属性 EX:如果InputStream.Length = 41230,则InputStream.Position也设置为41230,这不应该发生。理想情况下应该为零。

在我的代码中,我想出了实际设置位置等于长度

的线
    HttpPostedFileBase file = Request.Files["LayoutLevels[" + i + "].FloorwiseViewPath"];
//Here file.InputStream.Position = 0 which is desired
                    if (file.ContentLength > 0)
                    {
//below line causes file.InputStream.Position to change it equal to file.InputStream.Length
                        using (System.Drawing.Image image = System.Drawing.Image.FromStream(file.InputStream, true, true))
                        {
                            if (image.Width <= 720 && image.Height <= 550)
                            {...

using语句导致position属性从0更改为Length。

我可以在using语句中手动将position属性重置为0, 但我的问题是,为什么这个职位正在改变,以及避免改变这个职位的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

正在读取流以创建image,这就是位置结束的原因。这不是问题,也不是您需要缓解的问题。您现在可以根据需要操作Image,然后保存到Stream以获取上传到Azure的字节。

CloudBlobContainer container = //however you get your container
CloudBlockBlob blkBlob = container.GetBlockBlobReference(your_blobname);
//blobbytes would be the byte array acquired from image.Save(some_other_stream, imgFmt)
blkBlob.UploadFromByteArray(blobbytes, 0, blobbytes.Length);