How does the .gdoc format work? - file

If you work with Google Docs your first file format is .gdoc / .gsheet / .gwhatever
What I want to understand is, how this file format works. When you open a .gdoc you basically open the browser and go to a specific URL. So is a .gdoc just a .html-file with a changed file extension?
I would like to build something similiar: A rich-text-editor which saves the content in my own file format, that you can download and if you open the file, the browser opens and links to a specific URL.
BTW: I opened a .gsheet in a text editor and found this source code:
{"url": "https://docs.google.com/a/test.com/spreadsheet/ccc?key=01234567898765432123456789&usp=docslist_api", "resource_id": "spreadsheet:0A12345B678HJK9TZPL9078767"}
*Changed the URL a bit ;)

.gdoc and .gsheet files don't contain html, but JSON. These extensions are usually linked to the Google Drive application, which just reads the file and opens the URL with your standard browser.
If you're going to implement something like this yourself, you can go the same route: let the user install a program that handles your own files and start e.g. the browser.
Another way is to just use standard shortcuts; or use can use html files (maybe with another file extension if you link this extension to the standard browser) which just redirects to your target url.
in response to your comments:
to create a simple shortcut, you'll have to generate a file with the following content:
[InternetShortcut]
URL=your.url.com
and save it as YourFileName.YourExtension.url. Note that the "real" file-extension is .url. When saved to disk, windows explorer will usually hide the .url part (regardless of the Hide known file types setting). When the user starts this file, it will just open your.url.com. Chrome provides an API to create and save files client-side, but I don't know about other browsers. Probably you'll have to use a third-party library for creating the files (if you want to do this client-side).
Note this is a Windows-only solution. I don't know how you would handle this on Mac OS.

Related

dart: How to save a file?

I need to save the file that is on my hard drive.
File address: /home/mk/proj1/1.txt
Program address: /home/mk/proj2/
I try to save:
new dom.AnchorElement(href: '/home/mk/proj1/1.txt')
..setAttribute("download", '1.txt')
..click();
Displays information about saving, but writes: Failed! No file.
How to save a file?
There is not really a way to save a file in the browser (except perhaps limited support for temporary files)
You can search for JavaScript solutions. They will be quite similar for Dart in the browser.
For example JavaScript: Create and save file
If you want to store a file on the client machine, you can provide a download link.
See also How to force a Download File prompt instead of displaying it in-browser with HTML?
To provide a download link for data that is available in the browser clients code instead of a server file, you can use a data url.
See also https://stackoverflow.com/a/29691487/217408

Invoke File save as prompt while Downloading in ExtJS

I want to invoke the file save as prompt while allowing the user to download a file in ExtJS from a URL.
Basically the idea is to allow the user to change the file name and select his desired location before saving the file on their machine.
Is there any way I could do this?
I do not want to use:
A server side code to send the headers.
Nor do I want to use the Flash-based Downloadify library to do this.
Also, <a> tag's "download" attribute does download the file, but it does not prompt the dialog box if the browser settings are not set right.
Is there any way apart from the above where we could handle the file download in our application?
No, it cannot be done with javascript alone due to the security restrictions of the contemporary browsers. You can read files but I have no knowledge of a way to save files directly from the javascript alone.
If your "I do not want tos" are not that strict, here you can find an example of file downloading. The essence is to send "Content-Disposition: attachment" header.

Access Specific PDF Page in WPF WebBrowser Control

I have a webbrowser control in my application that is used to display pdf files that have been created in iTextSharp and are stored locally on the hard drive.
I would like to be able to navigate the file (next, previous, first, last, toc) from my application rather than using the inbuilt nav of the reader in the browser.
I have seen that you can navigate to specific pages by using
Browser.Navigate("filename.pdf#page=?);
This works the first time but when trying to navigate to a different page, it makes the browser disappear completely with no errors. However, I can reload the file without problem if I don't have the #page=? suffix on the file url though. Any ideas on this?
Alternatively, is there anyway in iTextSharp of adding something to the file to allow for it to be navigated from an external command?
All the official parameters that can be used to navigate through a PDF using parameters in the query string after the ? character are listed in a document published by Adobe: Parameters for Opening PDF Files
You already mentioned the page parameter. Another option could be using named destinations: nameddest=destination. In this case, you need to add the anchor with name destination to the file using iTextSharp.
Note that not all viewers implement these parameters. Adobe supports them in Adobe Reader and in the Adobe Reader plug-in, but there is no guarantee that they will work in pdf.js (Firefox), Pdfium (Google Chrome),... If your browser disappears when using an open parameter, you may have hit a bug in the browser or the viewer plug-in that causes the browser to crash. iTextSharp nor iText can crash a browser ;-)
There are no other ways you can navigate a PDF from an external application. The only thing you can do, is to add JavaScript to the PDF so that it always opens at the same page. This is done using an open action. I don't think this solves your problem as it would mean that you have to change the PDF file every time you want it to open at a different page.

What determines whether a browser will open or download a .txt file?

Let me start by saying that I am aware that you can specify the Content-Disposition in the header and use either "attachment" or "inline" and this question is NOT about that (at least not directly).
I have a file with a .txt extension. I am noticing different browser behavior dependent on the content of that .txt file. If my file contains alphanumeric characters and I paste the location of the file into the URL bar of (say) Chrome, the file opens in the browser.
If my file contains an "SI" character the file is downloaded instead of opened in the browser.
At first I thought it might be because "SI" is a control code, but "CR" and "LF" are also control codes and the file displays in the browser when they are present. The file even opens in the Chrome when there is a "DC2" character present in the .txt file.
My question is: At the file content level, what determines whether or not a file is displayed or downloaded?
(I realize this may vary by browser, but if there is something that's at least somewhat reliable, it would be helpful to know)
Thanks in advance.
EDIT (based on answer from Sam):
When the "SI" or "DC2" character is in the first 1000 or so characters of the text file it will be downloaded, if those characters are after the first 1000 or so characters the file is displayed in the browser.
The behavior of the browser when you send it a file is going to be based on the content-type header sent by your server. If you aren't explicitly setting it yourself, your web server may attempt to make a best guess based on the first few bytes of the file.
See http://httpd.apache.org/docs/2.2/mod/mod_mime_magic.html
The order how is determined if Save dialog is shown by a browser.
Content-Disposition set by web developer in Apache or cgi. By specs this is what we should do.
Content-Type header's MIME type set by web developer in Apache or cgi
application/octet-stream ... browsers ... treat it as if the Content-Disposition header was set
to attachment, and propose a "Save As" dialog.
Content-Type header's MIME type determined and set by a web server. e.g. Apache has two methods:
Apache's assign MIME type by mapping patterns in the URI or filenames. e.g. AddType application/octet-stream .pkg
Apache's mod_mime_magic, if it's installed and enabled by web developer
... This module is active only if the magic file is specified by the
MimeMagicFile directive.
HTML5 download attribute set by web developer in html file, or in JS (I did not test if it does not override the above)
If none of the above applied, the browser is going above specs and deciding by itself.
In the following screenshot, response headers don't mention Content-Disposition, nor Content-Type. On the left side we can see a part of the file was transferred (I did not click on Open or Save) and we can see browser stating it's "application/octed-stream"
My assumption from this is that browser after receiving a part of the file decided it would not be sane to show it inline (on the screen), declared the MIME and offered the Save dialog.
The screenshot was made from Developer Tools of Firefox after I pasted URL of the file in Omnibox, opened DevTools and pressed Enter in Omnibox to "go" to pasted address.

How do you link to a file such that it will download rather than render in browser?

I want people to be able to click my link and download my file. But when I link to the file download on my site, it just brings them to the code for the file. I am trying to upload a .cpp file (c plus plus). It's in my downloads folder but when I link to there it displays the .cpp file rather than download it.
Any help is appreciated.
You can set the Content-Disposition header to "attachment" in your HTTP response. If you let me know what server platform, I'll try to give more detail.
You will have to write some code to set the HTTP response header ContentType to "application/x-force-download". This tells the browser what you really want is a file download, instead of the default action (file open). You will also need to set the ContentLength header. This allows the browser to display a progress bar.
If you use ASP.NET, there are some commercial product for this purpose:
http://www.essentialobjects.com/Products/EOWeb/Downloader.aspx
It basically does what stated above with a few extra features.
That's because the browser knows how to read the file - you could ask users on the page to right click on the link and click "Save Target As" rather than just having a link.

Resources