Open window with pdf or html content from disk (external to iis)

  1. #1

    Open window with pdf or html content from disk (external to iis)

    Hello!
    i need open document from external path my server in new window..
    on my grid i have command column and after click pass in direct event 2 param : ext ( pdf or html) and full path to them

     <ext:CommandColumn ID="CommandColumn1" runat="server" Width="60" >
                            <PrepareToolbar Fn="prepareToolbar" />
                              <Commands>
                                   <ext:GridCommand Icon="Page" CommandName="View">
                                               <ToolTip Text="Посмотреть" />
                                    </ext:GridCommand>
                                </Commands>
                           
                           <DirectEvents>
                               <Command OnEvent="ShowDoc"  Success="onAfterShowDoc"  IsUpload="true" >
                                  <%--  <EventMask ShowMask="true"  />--%> 
                                            <ExtraParams>
                                                <ext:Parameter Name="ext" Value=record.data.col6 Mode="Raw" />   
                                                <ext:Parameter Name="link" Value=record.data.col1 Mode="Raw" />
                                            </ExtraParams>
    
                               </Command>
                           </DirectEvents>
    on the server side i found 2 examples..
    this work for me.. but open dialog - where i must select open or save..


      protected void ShowDoc(object sender, DirectEventArgs e)
        {
    
            string ext = e.ExtraParams["ext"];
            string link = e.ExtraParams["link"];
       
         FileInfo file = new FileInfo(link);
            // Checking if file exists
            if (file.Exists)
            {
                Response.ClearContent();
                Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                Response.AddHeader("Content-Length", file.Length.ToString());
                if (ext == "pdf")
                    Response.ContentType = "application/pdf";
                else
                    Response.ContentType = "application/html";  
                Response.TransmitFile(file.FullName);
                Response.End();
            }
        }
    and two examples - open content in window ( the same what i want)

      protected void ShowDoc(object sender, DirectEventArgs e)
        {
    
            string ext = e.ExtraParams["ext"];
        
            string link = e.ExtraParams["link"];
     
    
               var win = new Ext.Net.Window()
                {
                    ID = "newDocument",
                    Title =ext,
                    Width = Unit.Pixel(1000),
                    Height = Unit.Pixel(600),
                    Modal = true,
                    Collapsible = false,
                    Maximizable = true,
     
                    BodyStyle = "padding:10px;",
                    AnimateTarget = sender.ToString(),
                    Icon = Icon.PageWhiteAcrobat,
                    Loader = new ComponentLoader
                    {
                        Url = link, // this link only internal for IIS.??. i want  open from net share path.. html and pdf file
                        Mode = LoadMode.Frame,
                        LoadMask = { ShowMask = true },
                        MonitorComplete=true
                    }
          
             };
               win.Render(this.Form1);
           }
    may be help me edit 2 example.. if link to file inside server ( like 1.pdf) this work fine, but if link="G:\wert yuio\asdd 2323\232 555.pdf " - not open..
    thanks..
    Last edited by asics167; Jan 10, 2014 at 4:27 AM. Reason: edit proc 2
  2. #2

    this work for me and ok!

    create hidden window - where show request file ( in my system has only 2 type - htm and pdf)
    win has Loader with Mode=Frame and Url -> /handler/handler1.ashx - return file with header, path and type file via paramerters

     <ext:Window ID="wndShowDoc" runat="server" Title="" Icon="UserHome" BodyPadding="0"  AutoScroll="true"  TagString="iframe"
               Width="900" Height="600"  Modal="true" Resizable="true" Closable="true" Hidden="true" Layout="Fit" CloseAction="Hide">
                <Items>
                    <ext:Panel runat="server" ID="ctl39">
                        <Buttons>
                           <ext:Button runat="server" Icon="Decline" Text="Закрыть" ID="ctl195" >
                                        <Listeners>
                                            <Click Handler="this.up('window').hide();" />
                                         </Listeners>
                            </ext:Button>
                          </Buttons>
                         <Loader ID="Loader1" 
                                        runat="server" 
                                        AutoLoad="false"
                                        Url="~/Handler/Handler1.ashx" 
                                      
                                        Mode="Frame">
                                    <Params>
                                        <ext:Parameter Name="ext" Value="html" Mode="Value" />
                                        <ext:Parameter Name="link" Value="c:\1\1.html" Mode="Value" />
                                  </Params> 
                              <LoadMask ShowMask="true" />
                        </Loader>
                    </ext:Panel>
                </Items>
             </ext:Window>

    Handler1.ashx - in subfolder Handler open file, create Handler and return via Http ( was search on forum and modified):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.SessionState;
    using System.IO;
    
    namespace PatientCabinet.Handler
    {
        public class Handler1 : IHttpHandler, IReadOnlySessionState
        {
    
            public void ProcessRequest(HttpContext context)
            {
                HttpResponse response = context.Response;
                HttpRequest request = context.Request;
    
                response.Clear();
    
               string fullpath = "";
               string ext = "";
                if (!string.IsNullOrEmpty(request["link"]))
                    fullpath = request["link"];
                if (!string.IsNullOrEmpty(request["ext"]))
                    ext = request["ext"];
    
                WriteImage(response, fullpath, ext);
    
                response.Flush();
                response.Close();
    
            }
    
            private void WriteImage(HttpResponse response, string  fullpath, string ext )
            {
                using (MemoryStream imageStream = ReadFile(fullpath))
                {
                    if (imageStream != null)
                    {
                        response.AddHeader("Content-Length", imageStream.ToArray().Length.ToString());
                        response.ContentType = ext == "htm" ? "text/html" : "application/pdf";
                        response.BinaryWrite(imageStream.ToArray());
                    }
                }
            }
    
            private MemoryStream ReadFile(string fullpath)
            {
                string FileName = fullpath; 
            
                MemoryStream oStream = null;
                try
                {
                    if (File.Exists(FileName))
                    {
                        oStream = new MemoryStream(ReadLocalFile(FileName));
                        return oStream;
                    }
                }
                catch (IOException ex)
                {
                    throw (ex);
                }
    
                return oStream;
            }
    
            private byte[] ReadLocalFile(string file)
            {
                byte[] returnValue;
                byte[] returnBytes = null;
                try
                {
                    using (FileStream filestream = new FileStream(file, FileMode.Open, FileAccess.Read))
                    {
                        returnBytes = new byte[Convert.ToInt32(filestream.Length) + 1];
                        filestream.Read(returnBytes, 0, Convert.ToInt32(filestream.Length));
                    }
    
                }
                catch (Exception)
                {
                    returnBytes = null;
                }
                returnValue = returnBytes;
                return returnValue;
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    Main page - have Grid with list file in my directory - col1 - full path to file, have command column with Icon - click -Direct Event - open file in window describe upper

      <ColumnModel ID="ColumnModel1" runat="server">
                    <Columns>
                        <ext:RowNumbererColumn ID="RowNumbererColumn1" runat="server" Width="35" />
                        <ext:Column ID="Column1" runat="server" Text="col1" DataIndex="col1" Flex="1" Hidden="true"  />
                        <ext:Column ID="Column2" runat="server" Text="Дата" DataIndex="col2" Width="100"  />
                        <ext:Column ID="Column3" runat="server" Text="Место" DataIndex="col3"  Width="100"  />
                        <ext:Column ID="Column4" runat="server" Text="Тип" DataIndex="col4"  Width="100"  />
                        <ext:CommandColumn ID="CommandColumn1" runat="server" Width="60" >
                          <Commands>
                               
                                 <ext:GridCommand Icon="Page" CommandName="View">
                                    <ToolTip Text="Open" />
                                     
                                </ext:GridCommand>
                                                      
                                
                            </Commands>
                           
                           <DirectEvents>
                               <Command OnEvent="ShowDoc"   >
                                            <ExtraParams>
                                                <ext:Parameter Name="ext" Value=record.data.col6 Mode="Raw" />
                                                <ext:Parameter Name="link" Value=record.data.col1 Mode="Raw" />
                                                <ext:Parameter Name="date" Value=record.data.col2 Mode="Raw" />
                                                <ext:Parameter Name="name" Value=record.data.col5 Mode="Raw" />
                                            </ExtraParams>
    
                               </Command>
                           </DirectEvents>
                               
    
                          
                       </ext:CommandColumn>
    after click on CommandColumn - direct event ShowDoc:

     protected void ShowDoc(object sender, DirectEventArgs e)
        {
    
            string ext = e.ExtraParams["ext"];
         //   long Id = Convert.ToInt64(e.ExtraParams["id"]);
            string link = e.ExtraParams["link"];
            string date = e.ExtraParams["date"];
            string name = e.ExtraParams["name"]; 
            
            
            ctl39.ClearContent();
            ctl39.Loader.Params.Clear();
            wndShowDoc.SetTitle(date + ' ' + name);
            ctl39.Loader.Params.Add(new Ext.Net.Parameter("ext",ext));
            ctl39.Loader.Params.Add(new Ext.Net.Parameter("link", link));
            ctl39.LoadContent();
            wndShowDoc.Show();
            }
    Clear old File+ Old Paramete, set Title, add new Param - type file ( ext) and full Path ( link c:\qwerty\.. etc)
    call Loader - Handler1 with new Param and Show Window.
    Click image for larger version. 

Name:	file.jpg 
Views:	18 
Size:	95.6 KB 
ID:	7473
    Last edited by asics167; Jan 11, 2014 at 4:05 PM.
  3. #3

    Open window with pdf or html content from disk (external to iis) -

    I have tried replicating this solution in my application, and it works just fine in Internet Explorer.
    But with Chrome, it doesn't display the content of the files in the window, it only downloads the files.
    Would you know how can I get it to display file content in the window in Chrome?
  4. #4

    Chrome specifically blocks local file access this way for security reasons.

    dharamehta
    Chrome specifically blocks local file access this way for security reasons.
    https://stackoverflow.com/questions/...local-resource

    firefox,ie - ok
  5. #5

    Open window with pdf or html content from disk (external to iis) -

    That makes sense. Thanks asics167.
  6. #6

    Open window with pdf or html content from disk (external to iis) -

    I want to be able to open these files in a new browser tab instead of a modal window. Is there a way to do that?

Similar Threads

  1. [CLOSED] [#316] Window content disappears when moved
    By vadym.f in forum 2.x Legacy Premium Help
    Replies: 10
    Last Post: Jan 22, 2015, 10:44 AM
  2. Replies: 0
    Last Post: Jul 31, 2013, 3:23 AM
  3. [CLOSED] Force a external Page to open in a Window
    By Peter.Treier in forum 2.x Legacy Premium Help
    Replies: 1
    Last Post: Jun 13, 2013, 10:02 AM
  4. Replies: 3
    Last Post: Aug 21, 2012, 11:43 AM
  5. Replies: 1
    Last Post: May 28, 2010, 1:13 PM

Posting Permissions