2015-03-13 186 views
0

我试图将图像上传到azure blob,但是当我看到blob中上传的图像时,其大小为0字节。无法将图像上传到Azure blob

正如我想出,它正在发生,因为的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语句导致该位置属性从0改变为长度的线。

我可以使用语句, 内部手动将位置属性重置为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);