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

I want to be able to display the document in the browser without having to download it #2

Closed
CanerKarakurt opened this issue Apr 2, 2020 · 24 comments

Comments

@CanerKarakurt
Copy link

I have a scenario where I get an attachment from an exchange server and stream it back to the browser. I need to be able to render these files in the browser without having to download them on to the machine.

@shahzad-latif
Copy link
Contributor

@CanerKarakurt

Thanks for your query and sorry for late reply. We'll investigate this requirement and will get back to you soon.

@atirtahirgroupdocs
Copy link

@CanerKarakurt

Can you please specify the platform (.NET, Java) you're working on?

@CanerKarakurt
Copy link
Author

Hi @atirtahirgroupdocs working on .net

Regards

@atirtahirgroupdocs
Copy link

atirtahirgroupdocs commented May 5, 2020

@CanerKarakurt

Using GroupDocs.Viewer for .NET API you can render a documents without downloading it. API allows you to load source document from multiple sources (e.g. Amazon S3 storage, Azure Blob, Stream) and then render them into HTML, image or PDF formats.
By default GroupDocs.Viewer saves output results to the local disk but we also provide a way to save output results into a stream.
Once API renders source document and returns output, you can display that output in browser. Please let us know if this information meets your requirements. We'll then provide you a solution.

@CanerKarakurt
Copy link
Author

CanerKarakurt commented Aug 12, 2020

when I try to put a stream in the viewer I get following failure:

Cannot convert from “System.IO.MemoryStream” to
“GroupDocs.Viewer.Common.Func<System.IO.Stream>”

code:

       attachmentStream = new MemoryStream(attachment.Content);
        using (Viewer viewer = new Viewer(attachmentStream))
        {
            HtmlViewOptions viewOptions = HtmlViewOptions.ForExternalResources();
            viewer.View(viewOptions);
        }

@atirtahirgroupdocs
Copy link

@CanerKarakurt

Cannot convert from “System.IO.MemoryStream” to
“GroupDocs.Viewer.Common.Func<System.IO.Stream>”

Please have a look at the below code:

public static void Run()
{
      string outputDirectory = Utils.GetOutputDirectoryPath();
      string pageFilePathFormat = Path.Combine(outputDirectory, "page_{0}.html");
      using (Viewer viewer = new Viewer(GetFileStream)) 
      {
          HtmlViewOptions options = HtmlViewOptions.ForEmbeddedResources(pageFilePathFormat);
          viewer.View(options);
      }
      Console.WriteLine($"\nSource document rendered successfully.\nCheck output in {outputDirectory}.");
}
private static Stream GetFileStream() => File.OpenRead(TestFiles.SAMPLE_DOCX);

This is how you load a document from stream and then pass it to Viewer.

@CanerKarakurt
Copy link
Author

I've seen this however this requires you have a file in the file system I need to avoid that and load directly from a stream.

@atirtahirgroupdocs
Copy link

@CanerKarakurt

From which source/stream exactly you are loading the document (e.g. FTP, URL, S3 or blob storage)? Or can you please share your use-case/requirement.

@CanerKarakurt
Copy link
Author

Directly from Microsoft exchange server. Use case:

User receives an email with a bunch of attachments.
They then click on the attachment they want to view and view it directly in the browser without having to download it onto any machine. Think of the way outlook online works. You can view an attachment directly on the browser without having to download it.

Regards

@atirtahirgroupdocs
Copy link

atirtahirgroupdocs commented Aug 13, 2020

@CanerKarakurt

Thank you for the details. Please create a thread on our forum with reference of this GitHub issue. We'll then investigate it and you'll be notified on forum.

@CanerKarakurt
Copy link
Author

Done, I would need an answer soon as I have a client expectation. If it is not possible then I would have to look at other products.

Regards

@atirtahirgroupdocs
Copy link

@CanerKarakurt
We are investigating your use-case. You'll be notified as there's any update.

@atirtahirgroupdocs
Copy link

@CanerKarakurt

We have an update. In order make your code work, you have to pass stream factory instead of stream:

attachmentStream = new MemoryStream(attachment.Content);
using (Viewer viewer = new Viewer(() => attachmentStream))
{
    HtmlViewOptions viewOptions = HtmlViewOptions.ForExternalResources();
    viewer.View(viewOptions);
}

Let us know if it works for you.

@RakeshPatel2692
Copy link

I have the same problem. I am working with MVC5, I want to view document in view and I have memory stream in controller. please suggest the appropriate solution.

@atirtahirgroupdocs
Copy link

@RakeshPatel2692

I have the same problem. I am working with MVC5, I want to view document in view and I have memory stream in controller. please suggest the appropriate solution.

Please have a look at this thread.

@RakeshPatel2692
Copy link

Not able to download sample.zip
getting below error
"Sorry, this file is private. Only visible to topic owner and staff members."

@atirtahirgroupdocs
Copy link

@RakeshPatel2692
You can download the project here - sample.zip.

@RakeshPatel2692
Copy link

@atirtahirgroupdocs Please help me with MVC sample project . As I want to view document in MVC view. Already I have memory stream in my controller.

@atirtahirgroupdocs
Copy link

atirtahirgroupdocs commented Sep 4, 2020

@RakeshPatel2692
Please download a sample project here.

@RakeshPatel2692
Copy link

RakeshPatel2692 commented Sep 17, 2020

It works for me. Still I want show the document in div. Don't want to use Iframe. @atirtahirgroupdocs

@atirtahirgroupdocs
Copy link

@RakeshPatel2692

Please have a look at this comment.
However, you can explore this Angular based open-source project and it implements this npm package.

@RakeshPatel2692
Copy link

we are not using angular. Project UI is based on HTML+JQuery. Is there any way to show document in view.

@atirtahirgroupdocs
Copy link

atirtahirgroupdocs commented Sep 19, 2020

@RakeshPatel2692

You can pass the MemoryStream to view using ViewData and then render the output in Razor View.
Download or clone this application.
In HomeController and Index action, change following code:

return File(outputStream, "text/html");

with this:

ViewData["PageContent"] = outputStream;
return View();

And in Index View you can show document in div:

@using System.IO
@using System.Text
@{
    ViewData["Title"] = "Home Page";
} 
<div class="text-center">
    <h1 class="display-4">Render page on the server</h1>
    <div>
        @{
            MemoryStream stream = (MemoryStream)ViewData["PageContent"];
            string pageContent = Encoding.UTF8.GetString(stream.ToArray());
        }

        @Html.Raw(pageContent)
    </div> 
</div>

@shahzad-latif
Copy link
Contributor

The related project can be viewed here Display documents from a MemoryStream in ASP.NET MVC/.NET Core

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

No branches or pull requests

4 participants