Asked  6 Months ago    Answers:  5   Viewed   63 times

I'd like to have an html file that organizes certain files scattered throughout my hard drive. For example, I have two files that I would link to:

  • C:Programssort.mw
  • C:Videoslecture.mp4

The problem is that I'd like the links to function as a shortcut to the file. I've tried the following:

<a href="C:Programssort.mw">Link 1</a>
<a href="C:Videoslecture.mp4">Link 2</a>

... but the first link does nothing and the second link opens the file in Chrome, not VLC.

My questions are:

  1. Is there a way to adjust my HTML to treat the links as shortcuts to the files?

  2. If there isn't a way to adjust the HTML, are there any other ways to neatly link to files scattered throughout the hard drive?

My computer runs Windows 7 and my web browser is Chrome.

 Answers

97

You need to use the file:/// protocol (yes, that's three slashes) if you want to link to local files.

<a href="file:///C:Programssort.mw">Link 1</a>
<a href="file:///C:Videoslecture.mp4">Link 2</a>

These will never open the file in your local applications automatically. That's for security reasons which I'll cover in the last section. If it opens, it will only ever open in the browser. If your browser can display the file, it will, otherwise it will probably ask you if you want to download the file.

You cannot cross from http(s) to the file protocol

Modern versions of many browsers (e.g. Firefox and Chrome) will refuse to cross from the http(s) protocol to the file protocol to prevent malicious behaviour.

This means a webpage hosted on a website somewhere will never be able to link to files on your hard drive. You'll need to open your webpage locally using the file protocol if you want to do this stuff at all.

Why does it get stuck without file:///?

The first part of a URL is the protocol. A protocol is a few letters, then a colon and two slashes. HTTP:// and FTP:// are valid protocols; C:/ isn't and I'm pretty sure it doesn't even properly resemble one.

C:/ also isn't a valid web address. The browser could assume it's meant to be http://c/ with a blank port specified, but that's going to fail.

Your browser may not assume it's referring to a local file. It has little reason to make that assumption because webpages generally don't try to link to peoples' local files.

So if you want to access local files: tell it to use the file protocol.

Why three slashes?

Because it's part of the File URI scheme. You have the option of specifying a host after the first two slashes. If you skip specifying a host it will just assume you're referring to a file on your own PC. This means file:///C:/etc is a shortcut for file://localhost/C:/etc.

These files will still open in your browser and that is good

Your browser will respond to these files the same way they'd respond to the same file anywhere on the internet. These files will not open in your default file handler (e.g. MS Word or VLC Media Player), and you will not be able to do anything like ask File Explorer to open the file's location.

This is an extremely good thing for your security.

Sites in your browser cannot interact with your operating system very well. If a good site could tell your machine to open lecture.mp4 in VLC.exe, a malicious site could tell it to open virus.bat in CMD.exe. Or it could just tell your machine to run a few Uninstall.exe files or open File Explorer a million times.

This may not be convenient for you, but HTML and browser security weren't really designed for what you're doing. If you want to be able to open lecture.mp4 in VLC.exe consider writing a desktop application instead.

Tuesday, June 1, 2021
 
Francisunoxx
answered 6 Months ago
42

I've used local storage like this:

// To store a value
window.localStorage.setItem('key', value);

// To retrieve a value
value = window.localStorage.getItem('key');

// To delete a storage
window.localStorage.removeItem('key');

Hope that helps.

Monday, September 6, 2021
 
edA-qa mort-ora-y
answered 3 Months ago
10

You can go along the following lines:

class YourAdmin(ModelAdmin):   
     # add the link to the various fields attributes (fieldsets if necessary)
    readonly_fields = ('download_link',)
    fields = (..., 'download_link', ...)

    # add custom view to urls
    def get_urls(self):
        urls = super(YourAdmin, self).get_urls()
        urls += [
            url(r'^download-file/(?P<pk>d+)$', self.download_file, 
                name='applabel_modelname_download-file'),
        ]
        return urls

    # custom "field" that returns a link to the custom function
    def download_link(self, obj):
        return format_html(
            '<a href="{}">Download file</a>',
            reverse('admin:applabel_modelname_download-file', args=[obj.pk])
        )
    download_link.short_description = "Download file"

    # add custom view function that downloads the file
    def download_file(self, request, pk):
        response = HttpResponse(content_type='application/force-download')
        response['Content-Disposition'] = 'attachment; filename="whatever.txt"')
        # generate dynamic file content using object pk
        response.write('whatever content')
        return response
Thursday, September 23, 2021
 
Paul Stanley
answered 2 Months ago
66
useful = []
with open ("Report.txt", "r") as myfile:
    for line in myfile:
        if "===" in line:
            break
    for line in myfile:
        useful.append(line)
a_string = "".join(useful)

I would however prefer to hide it away in a generator, like this:

def report_iterator():
    with open ("Report.txt", "r") as myfile:
        for line in myfile:
            if "===" in line:
                break
        for line in myfile:
            yield line

for line in report_iterator():
    # do stuff with line

All the filtering and nitpicking is done in the generator function, and you can separate the logic of "filtering input" from the logic of "working with the input".

Friday, November 19, 2021
 
c0dehunter
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share