Skip to content

Commit f707954

Browse files
Merge pull request #339 from microsoft/PSL-BUG-16600
fix: Text file getting downloaded on click of details button
2 parents 0d37499 + 7961447 commit f707954

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

App/frontend-app/src/components/documentViewer/documentViewer.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export function DocDialog(
7575
const [iframeKey, setIframeKey] = useState(0);
7676
const [isExpanded, setIsExpanded] = useState(false);
7777
const [clearedChatFlag, setClearChatFlag] = useState(clearChatFlag);
78+
const [iframeSrc, setIframeSrc] = useState<string | undefined>(undefined);
7879
// const [aiKnowledgeMetadata, setAIKnowledgeMetadata] = useState<Document | null>(null);
7980

8081

@@ -90,6 +91,24 @@ export function DocDialog(
9091
setUrlWithSasToken(undefined);
9192
}
9293
}, [metadata]); // Only run when metadata changes
94+
95+
useEffect(() => {
96+
if (metadata && metadata.fileName.endsWith(".txt")) {
97+
fetch(metadata.document_url)
98+
.then((response) => response.text())
99+
.then((textContent) => {
100+
const blob = new Blob([textContent], { type: "text/plain" });
101+
const objectURL = URL.createObjectURL(blob);
102+
setIframeSrc(objectURL);
103+
104+
// Cleanup the object URL when component unmounts or metadata changes
105+
return () => URL.revokeObjectURL(objectURL);
106+
})
107+
.catch((error) => console.error("Error fetching text file:", error));
108+
} else {
109+
setIframeSrc(metadata?.document_url);
110+
}
111+
}, [metadata]);
93112

94113

95114
const downloadFile = () => {
@@ -236,7 +255,7 @@ export function DocDialog(
236255
<IFrameComponent
237256
className="h-[100%]"
238257
metadata={metadata}
239-
urlWithSasToken={urlWithSasToken}
258+
urlWithSasToken={iframeSrc}
240259
iframeKey={iframeKey}
241260
/>
242261
</div>

0 commit comments

Comments
 (0)