To load a document from a database try passing a stream instead of a document.Name like so
// Implement a method that returns a stream with document data.
Stream stream = GetStream(FileName);
// Render a document from the stream.
using (Viewer viewer = new Viewer(stream))
{
HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources();
viewer.View(viewOptions);
}
//
Stream GetStream(string fileName)
{
byte[] file = //TODO: read bytes from database
return new MemoryStream(bytes);
}
While Viewer can detect file type it is recommended to pass file type in the load options e.g.
// Implement a method that returns a stream with document data.
Stream stream = GetStream(FileName);
// Specify the file type
FileType fileType = FileType.FromFileName(FileName);
LoadOptions loadOptions = new LoadOptions(fileType);
//Render a file
using (Viewer viewer = new Viewer(stream, loadOptions)
...
Hallo Herr Litvinchik,
I wrote them but I get the this error: GroupDocs.Viewer.Exceptions.GroupDocsViewerException: ‘Access to the path ‘C:\Program Files (x86)\IIS Express\p_1.html’ is denied.’
[HttpPost]
public void PopPreview(string FileName)
{
Stream stream = GetStream(FileName);
using (Viewer viewer = new Viewer(stream))
{
HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources();
viewer.View(viewOptions);
}
**//return Json(viewer, JsonRequestBehavior.AllowGet); - What I write here? which return?**
}
private Stream GetStream(string fileName)
{
DBDocument document = new DBDocument();
document.DocId = Convert.ToInt64(fileName);
document.Open();
byte[] bytes = (byte[])document.Body;
return new MemoryStream(bytes);
}
Could you give me a lot information?
Best Regards
Sema
Vladimir Litvinchik via GroupDocs <forum@groupdocs.com>, 15 Haz 2023 Per, 16:36 tarihinde şunu yazdı:
By default, Viewer is trying to save output HTML files in the current folder. You can try saving the output into memory and sending it to the client as an HTML document see more at Save output to a stream documentation article, e.g.
[HttpPost]
public void PopPreview(string FileName)
{
MemoryStream document = new MemoryStream();
MemoryPageStreamFactory pageStreamFactory = new MemoryPageStreamFactory(document);
Stream stream = GetStream(FileName);
using (Viewer viewer = new Viewer(stream))
{
HtmlViewOptions viewOptions = HtmlViewOptions.ForEmbeddedResources(pageStreamFactory);
viewOptions.RenderToSinglePage = true;
viewer.View(viewOptions);
}
return new FileStreamResult(document, "text/html")
}
private Stream GetStream(string fileName)
{
DBDocument document = new DBDocument();
document.DocId = Convert.ToInt64(fileName);
document.Open();
byte[] bytes = (byte[])document.Body;
return new MemoryStream(bytes);
}
private class MemoryPageStreamFactory : IPageStreamFactory
{
private readonly MemoryStream _page;
public MemoryPageStreamFactory(MemoryStream page)
{
_page = page;
}
public Stream CreatePageStream(int pageNumber) => _page;
public void ReleasePageStream(int pageNumber, Stream pageStream) { _page.Position = 0; }
}
Hallo Herr Litvinchik,
I tried your answering but I get error this:GroupDocs.Viewer.Exceptions.GroupDocsViewerException: ‘Access to the path ‘C:\Program Files (x86)\IIS Express\p_1.html’ is denied.’
How can I fix it?
My scenario is: 1-)User chooses documents then clicks the Preview button
2-) I pushed this String(Document id) 3-) I opened them(from Database) I have many documents
4-) I want to show them on many Popups .
I did 1,2, 3 and 5 but I can’t show them. I searched for one component on the web. I found GroupsDocsViewer.
So I wrote your reply on Controller (after getting the Document from the database) but now I can’t show it.
Could you explain GroupDocsViewer? Is it suitable for my scenario?
Best Regards
Vladimir Litvinchik via GroupDocs <forum@groupdocs.com>, 15 Haz 2023 Per, 17:45 tarihinde şunu yazdı: