how to open MS Office word in asp.net? - asp.net

in my application, when user click on particular link button, MS word has to open how can i write the code for this. Thank you

There is no way to guarantee that a particular application will be opened when a user clicks a link / button on a web page. The application that is opened is determined by the user's browser and operating system settings.
As a developer you can specify the MIME type of the file that you are returning. By doing so you are telling the user's browser what file type is contained in the response. W3Schools provides a pretty good MIME type by content type list and FILExt also provides the MIME type for the files it lists.
Assuming you specify the appropriate MIME content-type you can be sure the user's browser and operating system will open the file in the "appropriate" application according to their settings. Since you want to open a Word Document file the appropriate MIME content-type will be one of the following:
Extension Type/sub-type
docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
doc application/msword
How and where you specify the content-type largely depends on the ASP.NET application type you are working with. If you are writing a ASP.NET Webforms application you'd change the MIME type of the Response object in the Page_Load method. In an ASP.NET MVC application you'd do so in a controller action. In either case the specific line of code is the same.
Response.ContentType = "application/msword";

When you click on a link, redirect the browser to the file mydoc.docx - the browser will open that in Word as long as they have Word installed. You will also need to maek sure your IIS server has the MIME type setup, see Downloading Docx from IE - Setting MIME Types in IIS
If you are generating the Word document on the fly, you will need to set the MIME type so that the browser knows that the response is a word document. I suggest the docx format for generating content.

If it is a Windows Application, this should work...
System.Diagnostics.Process.Start("FileName.doc");
For a web application, just redirect to the filename on the hosted server, or use one of the ways as described in the post below...
http://www.dotnetscraps.com/dotnetscraps/post/4-ways-to-send-a-PDF-file-to-the-IE-Client-in-ASPNET-20.aspx

Related

Icons/images not loaded in IE after adding “X-Content-Type-Options: nosniff” in web.config file

I am using ASP.NET platform to create a web page. Inside the page i have used some images/icons. For security purpose i have used “X-Content-Type-Options: nosniff” in web.config file. When i deploy the web page in IE, some of the images/icons isn't rendered. But, the same page working fine in Firefox and Chrome.
When i remove the statement “X-Content-Type-Options: nosniff” from web.config everything is working fine in IE. But, for security purpose i must use that statement. At the same time the missing images/icons need to be rendered in IE.
So, can anyone help me how to fix the issue with the statement “X-Content-Type-Options: nosniff".
Thanks in advance,
The nosniff only applies to "script" and "style" types. Also applying nosniff to images turned out to be incompatible with existing web sites.
So "X-Content-Type-Options nosniff" would bypass the problem for images and here comes the browser role which fail to render the image if the type mentioned by the server is not matching the real file extension.
Refer to:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options.
and this:
https://msdn.microsoft.com/en-us/library/gg622941(v=vs.85).aspx
IE uses MIME information to determine how to handle files sent by a Web server. For example, when Windows Internet Explorer receives a .jpg file, the user sees the file in an Windows Internet Explorer window. The MIME Handling Restrictions feature helps prevent script injection attacks against Web servers by ensuring that any content delivered with an IMAGE MIME is not treated as HTML or XML.
https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/dd565640(v=vs.85)
Add following line before saving bitmap
Response.ContentType = "image/gif";

open png/pdf in new tab rather than open download window

I ve to servers with my project. I would like to understand why there is a difference in the behaviour on these 2. On the first one whne i click on:
OPEN
the new tab is opened with pdf being rendered and on the other server(the same browser - chrome) new tab is opened but instead of starting rendering pdf download window appears.
Thanks for any sugestions and explanation
the server is IIS 6.0
The one that is downloading the content does not have MIME types are not configured properly.
It is treating the files are unrecognized static files.
Since the Content-Disposition header is not set it properly, the browser doesn't know it can render those types.
Steps to configure MIME types
The two servers probably send the PDF file with different MIME types in the header, because they are configured differently. If you want PDFs to be opened in the browser, the correct MIME type is application/pdf, as defined in RFC 3778.
Here is a step-by-step tutorial on how to configure MIME types in IIS 6.0:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/cd72c0dc-c5b8-42e4-96c2-b3c656f99ead.mspx?mfr=true
Seems like one of the browsers has a plugin available, or configured by mefor opening the document itself, while the other one doesn't (this might also mean that the MIME type of the file isn't properly configured so the browser doesn't know what to use to open the file).
If you want to force all browsers to show the download dialog (attachment) or trying to open it (inline), you can do so with the Content-Disposition header field. For instance:
Content-Disposition: attachment; filename="fileTitle.pdf"
or
Content-Disposition: inline;

Word hyperlinks not opening asp.net files

I have some Word files which need to have hyperlinks. The hyperlinks go to an htm file with an anchor, but that htm file isn't provided via a direct url for security reasons. Rather it is linking to a ashx handler file that retrieves the file and does an response.write to show the html file in the browser. Before it does this, though, it checks to back sure that there's a valid session, and if there isn't then it just redirects to the login page. This works fine when linking from within the ASP.Net site, but when I link to it from a local MSWord file, it apparently doesn't know there's a valid session (even though I've logged in in the browser), and redirects to the login page. Is there any way around this? For compatibility these Word documents need to be in Word 97-2003 format unfortunately...
No. This won't work.
Opening the word file outside of a browser and clicking on the link is going to start a brand new session; regardless of whether you currently have a browser window opened on the site.
Because a new session is starting, the web server will assume you aren't logged in at all. Which, technically, you aren't.

jpg file rendering problem in IE?

we wrote CMS apps with asp.net. the user can attach file documents, pdfs to their forms and send to each other. the user can easily download the pdf and other documents but when they want to download jpg file their IE render the jpg file as a binary and show the binaries of jpg file? the IE didn't show the save as dialogue such as other files!
where is problem? and how can I handle this in asp.net? Is it relate to HTTPS? because the user used the https to connect to this cms.
As I understand it, users need to download the files and they are able to do so for some files but not for JPEG? And it shows as binary, what do you mean, it shows the byte codes?
Keep in mind each request has a content type header in the response which will identify to the browser what kind of content it is. This is set a known MIME type such as image/jpeg, ...
Content-Type: text/html; charset=utf-8
Browser loads each resource based on the content type. Having said that, it is up to the browser to load the resource the way it wants. I can personalise and change my browser setting to some point so you have to look at the user's browser if you cannot reproduce it on each machine.
In any case, you need to see the content-type header in the response for the resource (using Fiddler as someone else also suggested) and make sure it is set to a correct value. If it is set to image/jpeg, it will render it as image but if it is set to application/octet-stream it will download it. My hunch is it is set to something else (such as text/html or text/plain) and that is why browser tries to show it as a text.

html code doesn't run in the browser, and only display the code

in order to find the server spec. i've created a file in the root dir in my website called spec.htm and entered this content as i was offered by another user:
<html>
<head>
<title></title>
</head>
<body>
#ServerInfo.GetHtml()
</body>
</html>
but i only get a copy of the code in my browser and it doesn't run it,
what the problem might be?
EDIT: i think that the problem is that i'm not using IIS.
is there a way to do so without using IIS?
thanks
Please see the following article
http://www.asp.net/webmatrix/tutorials/14-introduction-to-debugging
The ServerInfo helper is a diagnostic tool that gives you an overview of information about the web server environment that hosts your page. It also shows you HTTP request information that is sent when a browser requests the page. The ServerInfo helper displays the current user identity, the type of browser that made the request, and so on. This kind of information can help you troubleshoot common issues.
Create a new web page named ServerInfo.cshtml.
At the end of the page, just before the closing tag, add the following highlighted code.
#ServerInfo.GetHtml()
Note, it appears as though this is designed to run in IIS only and not on Linux / Apache servers.
Note, this is a RAZOR syntax so your system needs to be able to run Razor by installing the WebMatrix
#ServerInfo.GetHtml() is a Razor view engine syntax. Try saving your file as .cshtml or .vbhtml
The problem is that your webserver is not set up to serve HTML files through the ASP.NET interpreter. Change the extension to .aspx (i.e., use the same code, but call it spec.aspx).
Are you setting the Content-Type header correctly. If not set to text/html or similar, the browser or framework may set the content-type to text/plain which will not render the html at all.
You can check this in Firebug in the Net tab, expanding the response that is associated with the page you are serving, and looking in the Headers tab. If the Content-Type header is anything besides text/html or text/xhtml, then you need to find a way to make your web server set that header properly
let me guess its just showing up "#ServerInfo.GetHtml()" on a webpage. This does nothing if you put it simply in a body tag of a html page. If you are running IIS make sure you are saving as .aspx and not .html
See "yourhtmlsource.com/myfirstsite/myfirstpage.html"
I hope I am understanding the question and this helps. I found it on the web page given above.
When you double-click a file on your computer’s desktop, the computer knows what program to open the file in by checking the file’s “extension”. A txt file will open in a text editor.
You need to give your document a file extension of ”.html”, which will tell it to open the file in your web browser, such as Internet Explorer, Firefox or Safari.
Right now you should be editing your HTML page in a text editor, which normally saves files with the extension “.txt”. We want to make it save in “.html”. In your text editor click File → Save As…. If you use Microsoft Windows, there will be a box labelled “save as type”; change it to “all files .”. This means that you can save the data (in this case, some text) into any format. Now type in the name index.html for your file and click save. Ex: file.txt becomes file.html.

Resources