groupdocs-free-consulting / projects

0 stars 0 forks source link

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

Closed CanerKarakurt closed 3 years ago

CanerKarakurt commented 4 years ago

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 commented 4 years ago

@CanerKarakurt

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

atirtahirgroupdocs commented 4 years ago

@CanerKarakurt

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

CanerKarakurt commented 4 years ago

Hi @atirtahirgroupdocs working on .net

Regards

atirtahirgroupdocs commented 4 years ago

@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 commented 3 years ago

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

code:

       attachmentStream = new MemoryStream(attachment.Content);
        using (Viewer viewer = new Viewer(attachmentStream))
        {
            HtmlViewOptions viewOptions = HtmlViewOptions.ForExternalResources();
            viewer.View(viewOptions);
        }
atirtahirgroupdocs commented 3 years ago

@CanerKarakurt

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

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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

atirtahirgroupdocs commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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

atirtahirgroupdocs commented 3 years ago

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

RakeshPatel2692 commented 3 years ago

@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 commented 3 years ago

@RakeshPatel2692 Please download a sample project here.

RakeshPatel2692 commented 3 years ago

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

atirtahirgroupdocs commented 3 years ago

@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 commented 3 years ago

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

atirtahirgroupdocs commented 3 years ago

@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 commented 3 years ago

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