How can I create a download link for IPFS-hosted files? - javascript

I have been playing around with IPFS a lot recently, and have been wondering how to make a download link for files that gives them a custom name. The standard <a> tag download attribute doesn't work:
foo
Is there a way I can work around this by using JavaScript or Jquery? At a last resort I could route the files through the server, but I would prefer not to.

You can add your file by wrapping it inside a folder and therefore preserving the name of the original file. Try:
$ ipfs add -w example.txt
added QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH example.txt
added QmVFDXxAEC5iQ9ptb36dxzpNsQjVatvxhG44wK7PpRzsDE
This way, you can point to the last hash, which is a MerkleDAG Node that points to your file, preserving the name of it. Let me know if this solution works for you :)

Try adding "?filename=filename.pdf&download=true" at the end of CID.
Like so:
https://ipfs.io/ipfs/QmV9tSDx9UiPeWExXEeH6aoDvmihvx6jD5eLb4jbTaKGps?filename=filename.pdf&download=true

Related

How to automatically make array of all files in website folder?

Let's say I have a folder of about 100 images on my website called "IMG"
Now let's say I have a div element: <div id="templateDiv"></div>
Using javascript, how would I add all images from "IMG/" into that div without adding <img src="IMG/IMGNAME.jpg"> for every image?
Sorry, I'm not very good at explaining.
Just ignore the fact that would take ages to load.
EDIT
Ok my bad explanation skills have made me change my question.
How do I automatically make array of all files in website directory?
Okay, your question is incredibly unclear, but from reading all your other comments and things, it seems you simply want to get an array that contains the filename of every file in a directory? If that's what you want, then it won't be possible (I don't believe) since only the server knows which files are where, and you can't request the contents of a directory from a server using JavaScript.
However if you were using Node.js on a local directory, then it could be done. But I don't believe that's your case.
That being said, you have three alternative options:
Name every image file 1.png, 2.png, 3.png, etc. Then use a for loop and get each one using (i + 1) + ".png"
If you can't rename the files, but the files are named via user input, you could collect the user's input at the time of file creation and add the name of the newly created file into another file/an array/localStorage so that it could be retrieved later.
If you can't rename the files, but the filenames are also never known to the program that needs them, then you could create an array of all the filenames (manually) and iterate over that to find all the files that you want.
Please, somebody let me know if I'm wrong and if there actually is a way to make a request to a server that tells the client all the files in a directory. That seems incredibly unlikely though.
Another potential solution just came to mind. You could write a PHP script (or Node.js or any server-side language, really) that scans a directory, creates a list of all the filenames there, and then sends that back over HTTP. Then you could simply make an XMLHttpRequest to that PHP file and have it do the work. How does that sound?

Linking to and downloading a file where name changes? Best method, Regex?

I'm writing some html that links to a server to download a file. The location always remains the same, however, the file name gets updated.
Basically the link is like this:
http://www.somedomain.com/somedir/dir/filename_revA.pdf
The filename will often get revised ("filename_revB.pdf") for example. My goal would not having to change the html href code each time this happens.
TL;DR-- I want to be able to reference an url, ie: 'http://www.somedomain.com/somedir/dir/'
but not need the filename to fetch the file that is there after the last /.
Would RegEx (javascript) be best solution or a better method? Sorry if it's been asked I tried searching couldn't quite get what I wanted- requesting a URL WITHOUT the filename needed-while still fetching the actual file that's there. There's only one file that is there at all times, but perhaps to be safer using part of the name since only the last part changes?
edit- I don't have access to the server that hosts the file(s).

How can I generate a path relative to a Javascript/jQuery file?

I have a CMS template that uses JavaScript/jQuery to insert an image onto the page. This works fine when I specify an explicit path to the image, but because I use the template on several sites, the path needs to be determined automatically.
Making things a little harder is the fact that the path to the template (and therefore the image I'm linking to) changes periodically with each revision to the template.
Here's the (extremely simple) relevant code at the moment - which technically works in the short term, but is not the solution I'm looking for:-
src = '../template_v1/images/pdf_small.png'
This correctly generates the base URL, but breaks as soon as the template version is incremented (and the path changed) to template_v2 or template_v3, for example.
The JavaScript/jQuery file (again, included with the template) is located at http://www.domain.com/template_v1/js/this_file.js - so with that in mind, I want to be able to automatically generate a path to the image relative to the location of the this_file.js file. If this were CSS this would be easy, as non-explicit paths are relative to the CSS file calling the path - but I don't know how to accomplish this with JavaScript/jQuery.
Thanks in advance for any tips.
Do you need to go up a folder at the start of the source? Would going from the current directory work and stay within the template folder entirely:
src = './images/pdf_small.png'

Can you change the output location for the Randori generated html files?

I'm creating a new project and I want to customize the location of the generated files from Randori. How can I do this?
Check out Lesson-02: Creating an Initial HTML file & Run Configuration
From here you will see how to edit your Randori configuration. You can make the generated output anything you want. However just make sure to update your main html file and point to the correct (customized) generated folder.
One more point on this when you call the launch() method of RandoriBootstrap, you can pass two arguments. The first is whether you want debug mode on or off (debug mode does cache busting) the second is the URL that the class loader will use to find your classes. Should you put things in generatedFoo folder, you need to make sure the RandoriBootstrap knows this.

HTML5 FS API: Let the user create files outside of the sandbox?

Is there a way to create a file outside of the sandbox? Maybe something that works by first storing the file inside the sandbox and then letting the user drag a link to the locally stored file into a regular folder?
It is for a webchat: I want the user to be able to receive files, but I can't figure out a way that ensures that he can choose the target location that doesn't need a right-click->save as. If there is a better solution than using the filesystem API, feel free to suggest that, too.
How about an "a" element with a download attribute, and using FileEntry.toURL() to populate the href target?
Use a signed java applet to get root access to the system

Categories