HTML - open file code?

Discussion in 'OT Technology' started by dissonance, Mar 2, 2007.

  1. dissonance

    dissonance reset OT Supporter

    Joined:
    May 23, 2006
    Messages:
    5,652
    Likes Received:
    1
    Location:
    KS
    I am making an html log browser for work and am wondering what the code is to make it so when someone clicks an an .xls (Excel file) that it opens the file without promting for the option to open or save to disk. I was thinking it was an onMouseClick(),xx type code but can't find an example to look at.
     
  2. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I'm pretty sure there is no such code, at least not unless you want to learn PHP. I've been looking for an answer to this myself -- I've got some PDFs that I want to force the user to download. In for answers, if any exist.
     
  3. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    :ugh: the misinformation from deuse AGAIN.

    This would be client-side, not server-side. PHP has *NOTHING* to do with it.
     
  4. dissonance

    dissonance reset OT Supporter

    Joined:
    May 23, 2006
    Messages:
    5,652
    Likes Received:
    1
    Location:
    KS
    well, class is back... i'll look into it more at work tomorrow and if i find any thing positive about forcing download i'll make sure to post it
     
  5. Neuman

    Neuman New Member

    Joined:
    May 10, 2004
    Messages:
    3,541
    Likes Received:
    0
    Location:
    Honolulu, HI
    i'd assume this would be a difficult one to figure out. especially if you're dealing with vista and all their security settings..
     
  6. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    I don't think this is possible. Imagine the security risks if this were actually allowed.
     
  7. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Don't jump on my ass, bitch. I said I wasn't sure but that I hadn't found a definite answer. The one place I saw that did seem to have a solution was talking about using PHP; I don't know why, I didn't read far enough.

    Nice job not actually answering the question, AGAIN. :ugh: How does that show me up, just saying I'm wrong and then sneaking out the back, eh?
     
  8. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    If you just want to display the contents of the excel file in the browser, you can do it with a servlet. Save the excel file as CSV file, then set the content-type in the response header to "application/vnd.ms-excel". You might need to use a different MIME type but I pretty sure that one will work. Then when the user opens the file it will be displayed in the browser like a spreadsheet.

    If you want them to be able to manipulate the file in excel, I'm not sure how you do that other than have them d/l the file.
     
  9. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    that is browser and platform dependent... Not gonna be reliable in any capacity
     
  10. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    The only thing I can think of, is use the Content-Disposition header and you can set it to 'inline' to make it appear in the browser, or 'attachment', and specify a filename, to specify it to be downloaded.

    I can't remember how reliable this ends up being.

    And of course, for this to work, this is going to mean server-side stuff. Say if you want, you could make it a php script and you specify the path:

    Let's say, http://test.com/xls.php?filename=logs/blah.xls
    PHP:

    if( empty($_GET['filename']) ) die('No filename!');

    // Get the filename; here, do checks to ensure the user isn't trying to
    // haxor you and specify getting a file of the wrong type or directory
    // that we don't want them to get to.
    $filename $_GET['filename'];

    // Set the headers
    header('Content-Disposition: inline'true); // The second parameter overrides any existing Content-Disposition header
    header('Content-Type: application/vnd.ms-excel');

    // Spit back the xls binary data
    readfile'/base/path/' $filename );

    Another alternative, is you can have an .htaccess file in the directory where your .xls files are, and force the Content-Disposition header there. Of course this depends on if you're using Apache! Also it's even better practice to not use .htaccess files and specify everything in your config instead (good for performance for one).

    <Files *.xls>
    ForceType application/vnd.ms-excel
    Header set Content-Disposition inline
    </Files>

    I hope some of this might help. But don't know what servers or technology you're on.

    The key thing is the Content-Disposition header, and the sad thing is that it's going to be flakey anyway depending on the end-users setup, as has already been remarked.
     
  11. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    So it was PHP code that I almost learned. :squint: Guess I should've read the rest of the article.
     
  12. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    As long as the header is set correctly, this is something that is configured in a browser. For instance, in Catalyst I export CSV directly to excel by setting the headers to do so, with the code:

    PHP:
        # Set the response content type to text/csv
    $c->response->content_type('text/csv');
    $c->response->header('Content-Disposition' => "attachment; filename=report.csv");
    This makes it pop up in Excel, if IE/Firefox is so configured.
     
  13. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    what would be a more reliable suggestion?
     
  14. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Lots of people have open in Excel set by default. If they don't, it asks and they say yes.

    Not really a problem.
     
  15. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    there is not one. Essentially, you're giving the browser a file. what is done with the file is completely up to the browser and user's preference.
     
  16. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    In my situation, though, where I want the PDFs to download by default, it is somewhat of a problem.
     
  17. PocoDiablo

    PocoDiablo New Member

    Joined:
    Jul 11, 2004
    Messages:
    4,413
    Likes Received:
    0
    Location:
    NOVA
    You can totally do this. Here's some ColdFusion code I made a few weeks back that does that:

    Code:
    		<cfheader
    	        name="Content-Disposition"
    	        value="attachment; Filename=FILENAME.csv"
    	        >
    	
    	        <CFCONTENT
    	        TYPE="application/vnd.ms-excel"
    	        RESET="Yes"
    	        >SSN,CHCS Date,MEPR,Providers,Coder,Coded date,Patient,Coder Notes,E&M,PDx,Auditor notes,Provider Codes,Folder,Scanned date,Scanner<cfoutput>#chr(13)#</cfoutput>
    				<cfoutput query="q_Search">#Right(q_Search.patient_ssn2, 4)#,#DateFormat(q_Search.patient_service_date_ts, "mm/dd/yyyy")#,#q_Search.patient_mepr2#,#q_Search.patient_providers2#,#q_Search.patient_coder_Last_Name# #q_Search.patient_coder_first_Name#,#dateFormat(q_Search.patient_coded_ts, "yyyy/mm/dd")#,#q_Search.patient_last_name#  #q_Search.patient_first_name#,#q_Search.patient_coder_notes#,#q_Search.patient_em#,#q_Search.patient_pdx#,#q_Search.patient_auditor_notes#,#q_Search.patient_facility#,#q_Search.patient_folder_name#,#q_Search.patient_scanned_ts#,#patient_Scanner_Last_Name# #patient_Scanner_First_Name##chr(13)#</cfoutput>
    
     
  18. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    The point being: TYPE="application/vnd.ms-excel"

    Is that going to work in firefox?
     
  19. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Cold fusion?? Better to use something free like Java.

    Peyomp, I believe we demo'd this in class using that MIME type with firefox and a Java servlet.
     
  20. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    defining the MIME/Content-type is only going to tell the browser what the data is... It will still be up to the browser to determine how to handle the data.
     
  21. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I guess the question then becomes "are most browsers preconfigured to handle data a specific way, or will they follow the website's recommendation?" It's a safe bet that Joe User hasn't bothered to define how every type of file should be handled.
     
  22. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    the 90% rule would be that browsers are defined to "ask" what to do with a file. The exception is MSIE under Windows where a default/quick install of MS Office has been done... An office doc/spreadsheet will open in the browser.

    However, even this is not guaranteed (for example I have my Office configured to not do that) and different browsers (FireFox or Opera) or different platforms (OSX, Linux, etc) will behave differently.

    Bottom-line, this is a bad assumption to make.
     

Share This Page