@@ -20,8 +20,41 @@ export default class ResponseBody extends React.Component {
2020 let body , bodyEl
2121 url = url || ""
2222
23- // JSON
24- if ( / j s o n / i. test ( contentType ) ) {
23+ if (
24+ / ^ a p p l i c a t i o n \/ o c t e t - s t r e a m / i. test ( contentType ) ||
25+ ( headers [ "Content-Disposition" ] && ( / a t t a c h m e n t / i) . test ( headers [ "Content-Disposition" ] ) ) ||
26+ ( headers [ "content-disposition" ] && ( / a t t a c h m e n t / i) . test ( headers [ "content-disposition" ] ) ) ||
27+ ( headers [ "Content-Description" ] && ( / F i l e T r a n s f e r / i) . test ( headers [ "Content-Description" ] ) ) ||
28+ ( headers [ "content-description" ] && ( / F i l e T r a n s f e r / i) . test ( headers [ "content-description" ] ) ) ) {
29+ // Download
30+
31+ const isSafari = / ^ ( (? ! c h r o m e | a n d r o i d ) .) * s a f a r i / i. test ( navigator . userAgent )
32+
33+ if ( ! isSafari && "Blob" in window ) {
34+ let type = contentType || "text/html"
35+ let blob = ( content instanceof Blob ) ? content : new Blob ( [ content ] , { type : type } )
36+ let href = window . URL . createObjectURL ( blob )
37+ let fileName = url . substr ( url . lastIndexOf ( "/" ) + 1 )
38+ let download = [ type , fileName , href ] . join ( ":" )
39+
40+ // Use filename from response header,
41+ // First check if filename is quoted (e.g. contains space), if no, fallback to not quoted check
42+ let disposition = headers [ "content-disposition" ] || headers [ "Content-Disposition" ]
43+ if ( typeof disposition !== "undefined" ) {
44+ let responseFilename = extractFileNameFromContentDispositionHeader ( disposition )
45+ if ( responseFilename !== null ) {
46+ download = responseFilename
47+ }
48+ }
49+
50+ bodyEl = < div > < a href = { href } download = { download } > { "Download file" } </ a > </ div >
51+ } else {
52+ bodyEl = < pre > Download headers detected but your browser does not support downloading binary via XHR (Blob).</ pre >
53+ }
54+
55+ // Anything else (CORS)
56+ } else if ( / j s o n / i. test ( contentType ) ) {
57+ // JSON
2558 try {
2659 body = JSON . stringify ( JSON . parse ( content ) , null , " " )
2760 } catch ( error ) {
@@ -53,40 +86,6 @@ export default class ResponseBody extends React.Component {
5386 // Audio
5487 } else if ( / ^ a u d i o \/ / i. test ( contentType ) ) {
5588 bodyEl = < pre > < audio controls > < source src = { url } type = { contentType } /> </ audio > </ pre >
56-
57- // Download
58- } else if (
59- / ^ a p p l i c a t i o n \/ o c t e t - s t r e a m / i. test ( contentType ) ||
60- ( headers [ "Content-Disposition" ] && ( / a t t a c h m e n t / i) . test ( headers [ "Content-Disposition" ] ) ) ||
61- ( headers [ "content-disposition" ] && ( / a t t a c h m e n t / i) . test ( headers [ "content-disposition" ] ) ) ||
62- ( headers [ "Content-Description" ] && ( / F i l e T r a n s f e r / i) . test ( headers [ "Content-Description" ] ) ) ||
63- ( headers [ "content-description" ] && ( / F i l e T r a n s f e r / i) . test ( headers [ "content-description" ] ) ) ) {
64-
65- const isSafari = / ^ ( (? ! c h r o m e | a n d r o i d ) .) * s a f a r i / i. test ( navigator . userAgent )
66-
67- if ( ! isSafari && "Blob" in window ) {
68- let type = contentType || "text/html"
69- let blob = ( content instanceof Blob ) ? content : new Blob ( [ content ] , { type : type } )
70- let href = window . URL . createObjectURL ( blob )
71- let fileName = url . substr ( url . lastIndexOf ( "/" ) + 1 )
72- let download = [ type , fileName , href ] . join ( ":" )
73-
74- // Use filename from response header,
75- // First check if filename is quoted (e.g. contains space), if no, fallback to not quoted check
76- let disposition = headers [ "content-disposition" ] || headers [ "Content-Disposition" ]
77- if ( typeof disposition !== "undefined" ) {
78- let responseFilename = extractFileNameFromContentDispositionHeader ( disposition )
79- if ( responseFilename !== null ) {
80- download = responseFilename
81- }
82- }
83-
84- bodyEl = < div > < a href = { href } download = { download } > { "Download file" } </ a > </ div >
85- } else {
86- bodyEl = < pre > Download headers detected but your browser does not support downloading binary via XHR (Blob).</ pre >
87- }
88-
89- // Anything else (CORS)
9089 } else if ( typeof content === "string" ) {
9190 bodyEl = < HighlightCode value = { content } />
9291 } else if ( content . size > 0 ) {
0 commit comments