Saurabh Palatkar
Saurabh Palatkar

Reputation: 3384

Unable to upload image to Azure blob

I am trying to upload the image to azure blob, but when I see the uploaded image in the blob its size is 0bytes.

As I figured out, it is happening because the position property of the InputStream (HttpPostedFileBase.InputStream) is becoming equal to the Length property of InputStream EX: if InputStream.Length=41230 then the InputStream.Position is also set to 41230, which shouldn't happen. Ideally it should be zero.

In my code I figured out the line which is actually setting position equal to the length

    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)
                            {...

The using statement is causing the position property to change from 0 to Length.

I can manually reset position property back to 0 inside using statement, but my question is, why the position is changing and what is the correct way to avoid it from getting changed?

Upvotes: 0

Views: 444

Answers (1)

Crowcoder
Crowcoder

Reputation: 11514

The stream is being read in order to create the image, that is why the position is at the end. This is not a problem, or something you need to mitigate. You now have an Image that you can manipulate as needed, then save to a Stream in order to get the bytes for uploading to 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);

Upvotes: 1

Related Questions