How to make PDF from GitHub open in new tab? - html

I have a pdf file in a GitHub repo and an anchor tag that points to it (<a href="https://github..../file.pdf>link</a>) and when I click it, i download the file automatically.
How to make it open the PDF in a new tab, before downloading it (to the file system, just downloading it within the browser)?
I've tried adding target="_tab" and target="_blank" to the anchor, but all got me the same results?
Is it achieable using just HTML, or I need to use an external JavaScript library?

You shouldn't need to add target="..." whatsoever. I guess you're pointing to the wrong URL. GitHub provides access to the raw file through this URL pattern:
//github.com/<account>/<repo>/raw/<branch>/path/to/file.pdf
Note the raw part in the URL. Check if you got your URL right. A functional example:
Test PDF
You can test it clicking this link. A dialog will open asking you to specify where to download it.

Related

How to show File Saveas Dialog in Blazor Server App

I am new to Blazor and trying to show File Saveas Dialog as shown in following link on a button click.
Save as Image
The requirement is - upon clicking the Saveas button above Saveas dialog should be popped up where user can choose the destination of file and file name.
I have tried "enabling the setting to check the save location in the download settings of the browser" and it works. But we do not want to depend on the Browser settings.
Please add your thoughts on below..
Instead of depending on the browser settings is there any other way to show Saveas dialog?
Are there any open source Nuget packages available to help on this?
NOTE: I am using .NET 6.0 for building my application
Thanks in advance,
Bhargavi Gowri.
I also wanted to bring up a window to save a file in which the user could select a folder. Before that, the system automatically saved to the Downloads folder.
As I understood, there was no such possibility before, but now it is possible thanks to this api: https://caniuse.com/native-filesystem-api.
I found this solution in the answer to this question: https://stackoverflow.com/a/70001920/16740180.
It's worth noting that I use Blazor WebAssembly and not a Blazor Server. And I do not know if it will work for you.
Unfortunately, this doesn't work for mobile devices right now, but it works fine for windows. I hope this helps someone.
This isn't a Blazor thing. In web browsers, files are downloaded from links using <a> tag in HTML using the download attribute. Just create a link to your file:
<a href="path_to_file" download>Save</a>
Save
The path must be on the same server, but blob and data links will work as well.
If you do not suggest a name, the browser will use the original filename (possibly changed to remove symbols the OS doesn't allow in file paths).
https://caniuse.com/download
If you want your link to look like a button, then that's a different issue, and you can google or ask that.

How to provide a download link for a dxf inside a chm

I have HTML code that compiles into a chm, and occasionally I want to include a link to directly download a file... for example a small binary drawing file (extension .qid in my app) used as sample data for a tutorial in the chm. I have been doing this just fine for little drawing files by just providing a link like this...
some text
But my current problem is I have a little sample dxf that is to be used in this tutorial and when I provide a download link like this...
some text
...then I get a link ok, but when I click on it, it puts the dxf contents inline as text, rather than poppping up a download Save As dialog for some file at a path like mk:#MSITStore:wherever.dxf
Now I looked at HTML attribute doco and found a 'download' attribute which is meant to force the link to download, but it made no difference. I used this syntax...
<a href="relativepath/some.dxf" title="whatever" download>some text</a>
...which generated a chm with a link but ignored the attribute 'download'.
How can I force the href link to lead to a download dialog for a dxf file?
Please note CHM's are 20 years old. hh.exe is the HTMLHelp executable on Windows and associated with *.CHM files. It's just a shell that uses the HTML Help API and is really just hosting a browser window based on the old Internet Explorer in the HTMLHelp Viewer window. This is not based on Microsoft's browser EDGE!
You know, the HTML (!) Attribute directs new browsers to download the linked resource rather than opening it.
But - the download attribute is not supported by Microsoft Internet Explorer.
I tested linking from a single local HTML file too. Other browsers like Firefox, Chrome and EDGE also open a link to a local *.DXF file always as text file.
This also happens with embedded (compiled into a CHM file) *.dxf files.
So, you'll need to create a link to a ZIP file like e.g. some.dxf.zipinstead.
UPDATE:
This is working when the *.DXF file is not embedded and stored on a server: Test it for your needs by using in the old manner
test.dxf download

Hardcode file data into webpage for download

I have a pdf document, whose path i have hardcoded into my webpage.
I also have a download link, clicking which the user can download the file.
<a href="Path\to\file" download>Click</a> (Works in HTML5)
However instead of having to carry both the webpage code and file separately, is it possible to incorporate the file data into the webpage itself and form a downloadable file whenever the user clicks on the link?
You can try to base64 encode that file and add it as link url:
DOWNLOAD
But adding file source to HTML is worst thing you can do. Why you need that? Your HTML will be loaded very slowly. CTRL + U and console debugging will be extra slow.

Open html file in a new tab instead of downloading it

For my Trac plugin, I have made an export script which converts contents to a different format. The result is an HTML code.
When I click the link, some browsers open the HTML code in a new tab, while others offer to download it as a .print file, depending on their specific settings I think. Opening this .print file shows the same HTML page as opening it directly, but locally instead of from the server.
How can I force it to always open in a new tab?
I think it might be a mimetype issue. If it is, which mimetype can I use to tell the browser to open the HTML code directly? I am currently using text/html as mimetype.
EDIT: some more info
To give some more insight, adapting from a comment of mine below:
I do not create the link myself. The link is provided by Trac, the bug tracking software the plugin is for, and what I do is implement the method that creates the HTML code and let it return the HTML code along with the mimetype. Trac then returns the HTML code either as a file, or as a new tab, when clicking on that content conversion link. What I am searching for is a possibility to specify in the HTML code or mimetype that it gets opened in a new tab directly.
Maybe there is some kind of mimetype specifying the (HTML) text as an HTML web document instead of HTML file (if that distinction even exists).
Or an HTML/XML header or doctype specifying whether it gets downloaded or opened by a browser. I think the browser need to get that information from somewhere.
Or maybe there is an option to set in Trac.
I hope these ideas of mine about what could exist can help those of you who are versed with either or some of these to find a solution. I could not find a solution through my research yet.
If you have a link that "directly" opens (not in a new tab) and you want it to open a new tab, one way of doing it is
This will create a blank page, then paste the link there automagically and thus you will have a new tab with the desired page.

How to force a Download File prompt instead of displaying it in-browser with HTML?

Download
If I click Download button, this target blank is opening a new window.
But I need it to prompt a dialog for saving this file. How can I achieve this?
This is something that you cannot absolutely control with HTML itself.
If the user is having a browser with PDF reading capabilities (or a plugin) and the corresponding settings to open PDF files in-browser, the PDF will open like that.
The PDF opens in a new tab simple because of your target="_blank", which has nothing to do with a download prompt.
If you are using HTML5 you can use the download attribute:
Download
If you have a back-end service which you can control or you feel like fiddling with your Web Server, you can always look for setting the right Content-Disposition. See this SO question for some nice discussion on Content-Disposition.