Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swagger-UI - Uncaught TypeError when response includes an attachment #2439

Closed
nurandhawa opened this issue Oct 5, 2016 · 7 comments
Closed

Comments

@nurandhawa
Copy link

Hi Guys.

I found an issue where the swagger ui would keep loading when the response of a GET request contains an attachment. The response headers include Content-Disposition →attachment; filename=example.txt and Content-Type →text/plain; charset=UTF-8 along some others.
The error that I was getting was this: Uncaught TypeError: Failed to execute 'createObjectURL' on 'URL': No function was found that matched the signature provided.
To fix this, I had to make some changes in the swagger-ui.js.
@@ -23447,7 +23447,11 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({

     var type = contentType || 'text/html';
     var a = document.createElement('a');
     var href = window.URL.createObjectURL(content);
     var binaryData = [];
     binaryData.push(content);
     var href = window.URL.createObjectURL(new Blob(binaryData, {type: type}));
     var fileName = response.url.substr(response.url.lastIndexOf('/') + 1);
     var download = [type, fileName, href].join(':');

I am using swagger-ui-2.2.5. Can anyone confirm this error and if so, make the above mentioned changes and merge it?

Thanks.

@tomsaleeba
Copy link

I'm getting this error in Springfox 2.6 but AFAIK it uses swagger 2.2.5 completely untouched. I'm doing the same thing with Content-Disposition and it worked with Spring 2.5. I think this commit is what introduced the problem: 80e548d#diff-92930cfe0ffd368f7f1aec615128ca42L701

I assume that commit was made to fix something else so simply reversing the change might not be a solution :(

@fehguy
Copy link
Contributor

fehguy commented Oct 13, 2016

hmm, this was well tested. Can you point to a sample server so it can be reproduced?

@tomsaleeba
Copy link

Thanks for the quick reply. Luckily I can point to a sample server.

If you go to https://api.aekos.org.au/swagger-ui.html#!/Data_Retrieval_by_Species/speciesDataDotCsvUsingGET and enter Abutilon aff. leucopetalum for the species name and hit 'Try it out', it'll work (that is, return a result straight away). If you then change the download flag to true and try it again, you'll get the error in your JS console. This download flag is what triggers the Content-Disposition stuff to be set.

The method that's being called is https://github.com/adelaideecoinformatics/aekos-api/blob/master/src/main/java/au/org/aekos/controller/ApiV1SpeciesRetrievalController.java#L90

As I mentioned, I'm using Springfox 2.6 (which from springfox/springfox@487e974 I understand uses swagger 2.2.5) so hopefully it's applicable. I'm also kind of n00by in this area so if I'm doing it wrong, I'm happy to be shown the light.

@fehguy
Copy link
Contributor

fehguy commented Oct 13, 2016

Thanks--I'm testing now and will report back.

@fehguy
Copy link
Contributor

fehguy commented Oct 13, 2016

Should be fixed, and tested against your API. Please confirm!

@tomsaleeba
Copy link

It is indeed fixed. Thank you very much for the quick response and fix.

I've hacked together a fixed JAR and pushed to the server linked above so you can see the fix in action.

@satyakokku
Copy link

satyakokku commented Aug 9, 2019

MY WEBAPI CODE LOOKS LIKE ..even i am not able to download the excel file from SWAGGER UI.FILE IS CURRUPTED ERROR COMING PLEASE HELP ME
**HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

        response.Content = new StreamContent(stream);

        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
        {
            FileName = fileName
        };

        return response;**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants