Creating .htaccess file with Blob

Hi,

I use the following code to create an index.html file using my script. This script is used on desktop.

var mycontent = 'My content goes here...';
window.URL = window.URL || window.webkitURL;
var myfile = new Blob([mycontent], {type: 'text/html'});

$('#button').attr('href', window.URL.createObjectURL(myfile));
$('#button').attr('download', 'index.html');

I want to create .htaccess file similarly but when I use

var mycontent = 'My content goes here...';
window.URL = window.URL || window.webkitURL;
var myfile = new Blob([mycontent], {type: 'text/plain'});

$('#button').attr('href', window.URL.createObjectURL(myfile));
$('#button').attr('download', '.htaccess');

It creates htaccess.txt file. Any ideas how I can create a .htaccess file?

It seems that you’ve found a problem that windows has with dot files, which are a common feature of linux. I’m not aware of any solution, other than to leave a reminder such as ‘removeBeforeSaving.htaccess’

Thanks, I tried using a remainder, but Windows doesn’t allow to remove the remainder and save as .htaccess after the file is created with my script.

I can create and save a .htaccess file with Notepad with no issues, no idea why it doesn’t allow my script to do it.

In the “Save as” dropdown area, change the text file to instead say “All files”. It will then accept whichever file name you provide without forcing it to be a text file.

Sorry, as I said I can create a .htaccess file with Notepad with no issues. My script can’t and that is what I am trying to solve. Thank you.

Windows will not allow your script to do what you’re wanting to achieve.

The only reasonable way that I’ve found to achieve it, is to use:

$('#button').attr('download', 'removeThisPart.htaccess');

which results in the file type of the save dialog box being .htaccess, from where you can then remove the removeThisPart of the filename in the save dialog box, before continuing on to save the .htaccess file.

It’s not as easy as what you desire, but that’s due to a limitation of windows.

If instead you change to using Linux, then you will have no problem with .htaccess as that’s native to the unix-based operating system that you will then be using.

1 Like

Thanks for the detailed explanation. My script does not create a save dialog where you can edit the file name (is that possible?) so I guess that will not work for me.

EDIT: Sorry, it seems a browser setting I disabled earlier, I see the save dialog now on Chrome and Firefox. Thank you again, I will use your solution as it is really close to what I needed.

1 Like

Have you tried surrounding the filename in quotes - that’s the usual way to force windows to accept the filename as is without it trying to add an extension or remove the leading dot.

$('#button').attr('download','".htaccess"');

Just tried, it replaced the double quotes with dashes. It showed

-.htaccess-

in the save dialog.

You could try escaping with backslashes. But I fear this is likely impossible to do with JavaScript.
Windows has tight security for some things, even to the point where certain actions need to be “run as administrator”

Yes, windows does make some things like this quite tricky. It looks like the closest solution so far is to add filename text that is to be removed when at the save dialog box:

$('#button').attr('download', 'removeThisPart.htaccess');
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.