locked
Images not uploading properly in Azure blob RRS feed

  • Question

  • User-2097295820 posted

    When I upload image to Azure. The image uploads with a file size of 0 byte.

    All I see is an empty image. The same code as once worked but dont know what is wrong.

    I see image in the directory of my Azure Blob storage folder. But this image are invalid.

    The storage keys are ok. What is the problem seems to be a memory allocation issue from my app.

    Monday, May 9, 2016 3:02 PM

Answers

  • User753101303 posted

    Hi,

    Check perhaps imageToUpload.InputStream.Position and try to change back the position to 0 if it is not there already. My first though was an incorrect call. How do you call this from your controller?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2016 2:51 PM

All replies

  • User753101303 posted

    Hi,

    Maybe some kind of flushing issue (or incorrect Async code ?) Which approach do you use for this upload? We'll likely need to see some code to maybe figure out what could be wrong. What if you try the simplest test you can imagine? Is the content you are trying to post even correct or could it be that some exception is entirely ignored causing to upload a zero length image?

    Monday, May 9, 2016 4:54 PM
  • User-646145796 posted

    Hi,

    As PatriceSc said, the code is helpful for us to reproduce this issue, please provide the snippet code that related with uploading image. I would suggest you have a look at Azure official site to check how to upload file to Azure storage, you could choose developer language at the top of the site, here is the link: https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/#upload-a-blob-into-a-container

    Best Regards,

    Jambor

    Tuesday, May 10, 2016 12:57 AM
  • User-2097295820 posted

    I noticed that when I do a simple demo I can upload a image very well.

    But in areas were I put the upload script with other modules , take for example my action methods I end up uploading an image with 0 byte.

    I would paste my upload script below so you all see. But I can confirm that this code works for demos.

    public async Task<string> UploadFileAsync(string Container, HttpPostedFileBase imageToUpload)
            {
                string imageFullPath = null;
                if (imageToUpload == null || imageToUpload.ContentLength==0)
                {
                    return null;
                }
                try
                {
                    CloudStorageAccount cloudStorageAccount = Azureconnectstring.GetConnectionString();
                    CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                    CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(Container); //"advert"
    
                    if(await cloudBlobContainer.CreateIfNotExistsAsync())
                    {
                        await cloudBlobContainer.SetPermissionsAsync(
                            new BlobContainerPermissions {
                                PublicAccess = BlobContainerPublicAccessType.Blob
                            }
                            );
                    }
    
                    string imageName = Guid.NewGuid().ToString() + "-" + Path.GetExtension(imageToUpload.FileName);
    
                    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(imageName);
                    cloudBlockBlob.Properties.ContentType = imageToUpload.ContentType; 
                    await cloudBlockBlob.UploadFromStreamAsync(imageToUpload.InputStream);
    
                  // imageFullPath = cloudBlockBlob.Uri.ToString();
    
                    return imageName;
                }
                catch (Exception ex)
                {
    
                }
                
                return imageFullPath;
            }

    Tuesday, May 10, 2016 12:34 PM
  • User-2097295820 posted

    This upload module tends to work. But it seems that when I use it with my action methods. Images go into 0 bytes. which affect how images are stored. I started experiencing this when I configured azurestorage in my project. Because I could previous upload images  in my local directory but I dont know what is going on now.

    Just as I mention above I have a very simple upload code. It works perfectly well. I only experience this when I use it in my action methods.

    Tuesday, May 10, 2016 1:22 PM
  • User-2097295820 posted

    From the errors I google. I saw people with similar issues.

    probably passing a stream that hasn't been reset to the beginning.  

    That was the issue the suggested but I don't know how to correct the code to do this. That is reset like was suggested

    Tuesday, May 10, 2016 2:20 PM
  • User753101303 posted

    Hi,

    Check perhaps imageToUpload.InputStream.Position and try to change back the position to 0 if it is not there already. My first though was an incorrect call. How do you call this from your controller?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 10, 2016 2:51 PM
  • User-2097295820 posted

    I would try your suggestion. My Action methods are async codes. Let me reset first to 0. But based on my observation, the codes in the action methods executes all of them. except for the upload that send a 0 byte.

    Tuesday, May 10, 2016 3:29 PM