如何在Web浏览器中显示任何格式文件 [英] How I can display any format file in web browser

查看:163
本文介绍了如何在Web浏览器中显示任何格式文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中有一个ActionUrl,它包含所有格式的文件的地址。



现在点击任何文件应该在浏览器中打开(不要下载)



我尝试了它的代号,但它只适用于pdf格式。



我尝试过:



I have one ActionUrl in application in that it contains address of files of all formats.

Now on click of any file it should be open in browser(Not to download)

I tried bellow code for it but its only working for pdf format.

What I have tried:

public FileContentResult ViewFile(string filename)
{
   string OnlyFileName = Path.GetFileName(filename);
   string ext = Path.GetExtension(filename);

   var mimeType = "application/pdf";

   if (ext.ToLower() == ".png")
   {
      mimeType = "image/png";
   }
   else if (ext.ToLower() == ".docx")
   {
      mimeType = "application/ms-word";
   }
//

   var fileContents = System.IO.File.ReadAllBytes(filename);

   return new FileContentResult(fileContents, mimeType);
}

推荐答案

你不能......

即使你有一个应用程序要显示任何可能的文件格式,甚至所有那些应用程序都正确地在您的浏览器中注册,甚至所有这些应用程序都有一个插件来显示浏览器中的文件,用户仍然可以将选项排除在外并强行下载...记住!这是他/她的域名!
You can not...
Even you have an application to display any possible file format, and even all those applications properly registered with your browser, and even all those applications has a plugin to display files INSIDE the browser, the user still can rule the option out and force download... Remember! It is his/her domain!


Quote:

现在点击任何文件它应该打开在浏览器中(不下载)

Now on click of any file it should be open in browser(Not to download)



这是不可能的。

无论如何,必须首先下载客户端屏幕上显示的任何内容。

一旦下载,你无法阻止用户保存文件。


It is impossible.
Anything that is displayed on client screen must be downloaded first, no matter what.
And once downloaded, you can't prevent the user from saving the file.

Quote:

使用我的代码我已经能够在浏览器中打开pdf和png文件了,我无法用它打开Word文件。

with my code i am already able to open pdf and png files in browser, I am not able to open Word files with that.



你的代码没有任何魔法,任何浏览器知道如何在插件的帮助下显示png和pdf。没有浏览器知道如何显示word文件,因为它没有插件。



为了显示浏览器不支持的任何文件格式,你有2个vhoices:

- 在浏览器端,编写一个插件来显示该文件格式。

- 在服务器端,编写一个将文件转换为浏览器已知格式的翻译器喜欢html。


Your code don't do any magic, any browser know how to display a png and a pdf with help of a plugin. And no browser know how to display a word file, because there is no plugin for it.

In order to display any file format not supported by browser, you have 2 vhoices:
- on browser side, write a plugin to display that file format.
- on server side, write a translator that will transform the file to a browser known format like html.


请试试这个

Please try this
[HttpPost]
  public ActionResult ViewPDF()
  {
      string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
      embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
      embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
      embed += "</object>";
      TempData["Embed"] = string.Format(embed, VirtualPathUtility.ToAbsolute("~/Files/Mudassar_Khan.pdf"));

      return RedirectToAction("Index");
  }


这篇关于如何在Web浏览器中显示任何格式文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆