Azure File Storage - File is corrupted once uploaded
Asked Answered
R

1

0

I have this code which I'm using for uploading files in azure file storage container.

        var originalFileName = GetDeserializedFileName(result.FileData.First());

        var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName);

        var uploadFolder = "/AzureDocuments" + '/' + correctLoanId ;
        var patString = HttpContext.Current.Server.MapPath(uploadFolder) + "/" + originalFileName;

        if(!Directory.Exists(HttpContext.Current.Server.MapPath(uploadFolder)))
        {
            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadFolder + '/' + correctLoanId));
        }

        if (!File.Exists(patString))
        {
            File.Copy(uploadedFileInfo.FullName, patString);
        }

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));

        CloudFileClient fileClient = storageAccount.CreateCloudFileClient();

        CloudFileShare share = fileClient.GetShareReference("documents");
        CloudFileDirectory rootDir = share.GetRootDirectoryReference();

        CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(correctLoanId);

        sampleDir.CreateIfNotExists();

        CloudFile cloudFile = sampleDir.GetFileReference(originalFileName);

        try
        {
            //Open a stream from a local file.
            Stream fileStream = File.OpenRead(patString);
            cloudFile.UploadFromStreamAsync(fileStream);
            fileStream.Dispose();

        }
        catch (Exception ex)
        {
        }

The file is correctly uploaded and the correct size is shown in azure but when I'm downloading the file I'm getting error message that the file is corrupted.

Any idea if I'm doing something wrong?

Retroflex answered 27/9, 2016 at 10:10 Comment(6)
How are you downloading the file? Using SAS or downloading programmatically?Pita
I'm trying directly from azure portal and via link which has been created using the azure permissions but both ways same outputRetroflex
Just curious .... why are you not awaiting this call - cloudFile.UploadFromStreamAsync(fileStream);? Shouldn't it be await cloudFile.UploadFromStreamAsync(fileStream);?Pita
I'll change that. Do you think that will change things? ThxRetroflex
I would think so. Instead, try doing something like cloudFile.UploadFromStream(fileStream); i.e. changing it to a sync method instead of async. See if that helps.Pita
You were right, pls write the last comment as answer so I can mark that as correct. ThanksRetroflex
P
1

The reason your file is corrupted is because of the following line of code:

cloudFile.UploadFromStreamAsync(fileStream);

Essentially you're starting an async process but not waiting for it to complete. To fix, you could do either of the following:

Use sync version of this method:

cloudFile.UploadFromStream(fileStream);

Or, wait for async method to finish (recommended):

await cloudFile.UploadFromStreamAsync(fileStream);

Please note that if you're using async method, you would need to make the calling method async as well.

Pita answered 27/9, 2016 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.