2015-03-13 194 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);