Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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);
}
Posted
Updated 6-Jun-17 21:53pm
v3
Comments
F-ES Sitecore 7-Jun-17 4:15am    
To work on "any file" you can work out the MIME type from the extension by querying the registry. There is a code example of this in the of the answers here https://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension

As mentioned in the solutions here however, sending the right MIME type is all you can do, it is then up to the browser to work out what to do with that type, if it can show the file embedded then it will, otherwise it will launch the app registered for the file, or if none is found give you the download option.
Tbs.User 7-Jun-17 8:09am    
Yes Thanks for it. with this now I am getting mimeType from filename, But I need to open it anyhow.

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!
 
Share this answer
 
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:
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.

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.
 
Share this answer
 
Comments
Tbs.User 7-Jun-17 8:07am    
Mean while if I will create plugin to display file format then it will work only for my browser.

And if I am trying to transform word to html or pdf at server side I have to install MSWord first in server. otherwise it will gives error, right?
Patrice T 7-Jun-17 8:29am    
Just like for PDF file, you will have to distribute your plugin to all your customers.
Beware, MSWord is not done to be installed on server, and you will have problems when you will try to have more than 1 MSWord open at same time.
Tbs.User 7-Jun-17 8:37am    
So there is no safe way to do it, Right?
Patrice T 7-Jun-17 9:36am    
For MSWord, the easiest way is to save the file as an html.
Please try this
C#
[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");
  }
 
Share this answer
 
Comments
Tbs.User 7-Jun-17 3:09am    
Hi Ravi,
Thanks to Answer, But I said I want to open any type of file in browser.
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900