Friday, 19 July 2013

Open Word File Directly on Client's PC without Displaying Save Dialogue Box

Open Word File Directly on Client's PC without Displaying Save Dialogue Box


So Here I am with a trick to open the Word File directly on button click event without interrupting the user with that annoying Save Options Dialogue Box. So here is how we can do this,,,

Put a Button on your design page and write the following code on the button click event of that button control..




    protected void Button1_Click(object sender, EventArgs e)
    {
        string FullFilePath =Server.MapPath("WordFile\\demo.docx");
        FileInfo file = new FileInfo(FullFilePath);
        if (file.Exists)
        {
            Response.ContentType = "application/vnd.ms-word";
            Response.AddHeader("Content-Disposition", "inline; filename=\"" + file.Name + "\"");
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.TransmitFile(file.FullName);
        }
    }

Display the Content of Word File in Browser


Display the Content of Word File in Browser


We do most of the our Document related activities using Microsoft Office.. Most of the time we need to export the details from these files and display it our browsers. Most of them are related to excel file like importing excel to database or importing the excel content to gridview. Today I will be showing how to display the content of Word File(*.doc) to be rendered in your browser using ASP.Net.

First of all you need to reference this Microsoft.Office.Interop.Word to your Web Application.

Then include the namespace:

using Microsoft.Office.Interop.Word;

Then on the Page_Load Event write the following code:[Here I am using a word file named demo.docx which is in the folder WordFile inside my Web Application's Root Folder.]




        Microsoft.Office.Interop.Word.Application objWordApp = new Microsoft.Office.Interop.Word.Application();

        //ACCESSING THE WORD FILE PATH

        object objWordFile = Server.MapPath("WordFile\\demo.docx");

        object objNull = System.Reflection.Missing.Value;

        Document WordDoc = objWordApp.Documents.Open(

        ref objWordFile, ref objNull, ref objNull,

        ref objNull, ref objNull, ref objNull,

        ref objNull, ref objNull, ref objNull,

        ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);

        WordDoc.ActiveWindow.Selection.WholeStory();

        WordDoc.ActiveWindow.Selection.Copy();

        string strWordText = WordDoc.Content.Text;

        WordDoc.Close(ref objNull, ref objNull, ref objNull);

        objWordApp.Quit(ref objNull, ref objNull, ref objNull);

        // Asssigning word file value to bind the div

        DisplayWord.InnerHtml = strWordText;

Uploading Files Using Multiple File Uploads in the same Page


Uploading Files Using Multiple File Uploads in the same Page

 

Hello Guys,Today I will be showing you how to upload multiple files using multiple File Uploads in the same Page. There are other alternatives for this as well like as AJAX FileUpload with Progressbar or Jquery Fileupload; but I am just trying to achieve the same using Multiple File Uploads in the same Page.. So Here is how can we achieve the same:

Design Part:


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Multiple File Upload</title>
</head>
<body>

<form id="form1" runat="server">

<div lang="aa" style="font-family: 'Curlz MT'; font-weight: bold; text-decoration: blink; font-style: oblique; text-transform: inherit;"> 
<h1>Upload Multiple Files in Asp.net Using C# code</h1></div>

<div><asp:FileUpload ID="FileUpload1" runat="server" /> <br /> 
<asp:FileUpload ID="FileUpload2" runat="server" /> <br /> 
<asp:FileUpload ID="FileUpload3" runat="server" /> <br /> 
<asp:FileUpload ID="FileUpload4" runat="server" /> </div> 
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload Files" /> 
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 

</form>
</body>
</html>


Code Behind:



    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpFileCollection multipleFiles = Request.Files;
        for (int fileCount = 0; fileCount < multipleFiles.Count; fileCount++)
        {
            HttpPostedFile uploadedFile = multipleFiles[fileCount];
            string fileName = Path.GetFileName(uploadedFile.FileName);
            if (uploadedFile.ContentLength > 0)
            {
                uploadedFile.SaveAs(Server.MapPath("~/Files/") + fileName);
                Label1.Text += fileName + "Saved <BR>";
            }
        }

    } 

back to top