简体   繁体   中英

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?

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.

您还可以使用GetObjectSaveToFile方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM