Search code examples
c#openstackrackspace-cloud

How to get jpg file from CloudFiles on Rackspace using openstack.net


I'm running the below code in my controller in an asp.net mvc project. I want to enable the user to view or download the files that I store on Cloud Files on rackspace.

        var identity =
            new CloudIdentity()
            {
                Username = "username",
                APIKey = "apikey"
            };
        var storage = new CloudFilesProvider(identity);

        Stream jpgStream = new MemoryStream();
        storage.GetObject("files.container", "1.jpg", jpgStream);

        Stream pdfStream = new MemoryStream();
        storage.GetObject("files.container", "2.pdf", pdfStream);

        var jpgResult = File(jpgStream, "Image/jpg", "1.jpg");
        var pdfResult = File(pdfStream, "Application/pdf", "2.pdf");

The above code works when I return pdfResult. I get the correct file. But when I return the jpgResult, the browser downloads 1.jpg as an empty 0KB file.

Am I doing this the right way? Any idea what the problem might be?


Solution

  • Problem solved after I added:

            jpgStream.Position = 0;
            pdfStream.Position = 0;
    

    Before the File() call. As per the question: File is empty and I don't understand why. Asp.net mvc FileResult

    I don't know why this wasn't an issue with the pdf file.