How to download file from local directory using python - django

I want to download a file from the local directory when the user clicks on a particular link. How I can do it using python.

Try this on click of link:
location.replace('your_loacal_filepath')
For Example:
location.replace('/files/out.txt')

if you are rendering HTML, then you should read about the HTML download Attribute
Quoting
The download attribute specifies that the target will be downloaded when a user clicks on the hyperlink.
This attribute is only used if the href attribute is set.
else if(currentobj.filename != null){
var xy = '{% static "/documents/dataset/" %}' + currentobj.filename;
alert(xy);
var $tr = $('<tr><td style="padding:5px;"><a href="'+ xy +'"
target="_blank" download>' + currentobj.filename.slice(0,25) +
"....." +'</a></td> </tr>');
$tbl.append($tr);
}
just add download tag along the hyperlink
if you want to specify a specific name for the download file then just mention it like this
<a href="/images/myw3schoolsimage.jpg" download="w3logo">

You can try this.
The download attribute instructs browsers to download a URL instead of navigating to it.
Check out. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes

Related

How to set the default directory for wysisyg editor insertImage in flask

I'm building a CMS in flask and I have built a simple wysiwyg editor using execcommands for creating and editing posts, and everything is working. For the insertImage command I'm using an input element to open a directory and choose an image. It works except of course it opens my computers default folder. I want it to open the uploads folder in the static directory where user images are stored in flask. How?
I have searched through flask docs, Python handling files documentation and there's no mention of this. This is a project I'm doing for a class. I'm going above and beyond the requirements for this project but that's how I keep things interesting. I mean it's supposed to be a CMS right. Well, CMS's always have wysiwyg's that open the default "uploads" folder to insert media. Also, when creating my upload functions I found that when uploading files flask needs the absolute path. But when serving them the relative path is necessary.
Any point in the right direction would be greatly appreciated. Thank you.
Here's the structure
<div class="col-md-1 tools">
<a href="#" data-command='insertImage'data-toggle="tooltip" title="Insert Media"><i class='material-icons'>add_photo_alternate</i>
</a>
<div class="editorInputs">
<input type="file" name="media" id="insertImage"
accept="audio/*,video/*,image/*"/>
</div>
</div>
Here's my js script
$('.tools a').mousedown(function(e){
let command = $(this).data('command');
if(command == 'insertImage'){
$(this).next().children('input').trigger('click');
let input = $(this).next().children();
input.on('change', function(e){
let val = $(input).val();
document.execCommand(command, false, val);
})
}
});
Here's how my uploads file is configured in flask
app.config['SITE_UPLOADS'] = 'D:/Courses/Development/Programming/Python/LaunchCode/LC101/unit2/build-a-blog/static/site/uploads/'
app.config['ADMIN_UPLOADS'] = 'D:/Courses/Development/Programming/Python/LaunchCode/LC101/unit2/build-a-blog/static/admin/uploads/'
app.config['ALLOWED_IMAGE_EXTENSIONS'] = ['PNG', 'JPG', 'JPEG', 'SVG', 'GIF']
app.config['DATA_FILES'] = 'D:/Courses/Development/Programming/Python/LaunchCode/LC101/unit2/build-a-blog/data/'
app.config['RELATIVE_PATH_SITE'] = '../static/site/uploads/'
app.config['RELATIVE_PATH_ADMIN'] = '/static/admin/uploads/'
So, I realized that I have to create a function to pull images from the uploads folder, display them, get their URL and pass it to the execcommand. And I did.
First, create the gallery structure with radio buttons to view files. Then put the gallery in a bootstrap modal to fire when the execccomand insertImage link is clicked. Grab the URL of the checked image. pass it to the execcomand function in my js.
On the flask side get a list of all files in the uploads directory with os.listdir(absolute/path/to/directory), returns a python list of the files. Next create file urls and put info in a dict by looping over the filenames in the list and adding the relative path to the filename. Pass the dict to the jinja2 template and populate the gallery.
Finally, execute the js.
Here's my python code and js code.
def get_uploads():
list_images = os.listdir(app.config['ADMIN_UPLOADS'])
images = []
i =0
length = len(list_images)
while i < length:
img = {}
img['name'] = list_images[i]
img['url'] = os.path.join(app.config['RELATIVE_PATH_ADMIN'], list_images[i])
images.append(img)
i+=1
return images
Here's my js.
if(command == 'insertImage'){
$("#uploadsGallery").modal();
$('.ftco-animate').addClass('fadeInUp ftco-animated')
let check = $(this).next().find('input.form-check-input');
let val;
check.on('change', function(e){
val = $(this).val();
});
$('#insertImg').click(function (e) {
r.setStart(editDiv, lastCaretPos);
r.setEnd(editDiv, lastCaretPos);
s.removeAllRanges();
s.addRange(r);
document.execCommand(command, false, val);
check.prop('checked', false);
});
}

display static html asset in jekyll + github pages

I want to add a link to a static html page (which I have created, not hosted anywhere). I added the html file to my images folder (contains the images which are being rendered in my jekyll blog). Then I make a hyperlink to the html files which I just added in the images folder, like this:
<a href=https://raw.githubusercontent.com/USERNAME/USERNAME.github.io/master/images/time.vs.score.html>View plots here</a>
Where username is my github username. This does not open a new html page, instead I just see the plain text of the html file. How can I add static html files are part of the assets? Thanks!
Thats what raw.githubusercontent.com is made returning raw file.Files are returned with Content-Type:text/plain; and then displayed as text by your browser.
You'd better link to github pages published content at https://USERNAME.github.io/images/time.vs.score.html
I found an easy solution using this tool: https://rawgit.com/
just replace:
<a href=https://raw.githubusercontent.com/USERNAME/USERNAME.github.io/master/images/time.vs.score.html>View plots here</a>
with:
<a href=https://cdn.rawgit.com/USERNAME/USERNAME.github.io/master/images/time.vs.score.html>View plots here</a>

Sitecore: How to get a Hyperlink Manager Url from a General Link Field

In the sitecore Rich Text Editor, user can highlight some words and insert a hyperlink. User can select a media item and it will generate a url like ~/media/9A3E8962D4364D0A9F98178A9572CC08.ashx
I have a general link field, what code should I write to generate the above Url? I have tried GetMediaUrl and GetItemUrl method, but they both return the file location.
My goal is that I need to get a url from a field which look like ~/media/9A3E8962D4364D0A9F98178A9572CC08.ashx. Should I use a different type of field? The file which the link is pointing to is a pdf.
You should pass custom MediaUrlOptions to get desired URL format:
var mo = new MediaUrlOptions();
mo.UseItemPath = false;
var url = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem, mo);
UseItemPath indicates whether item path or item id should be used for URL.

Redirecting an external link in the menu so it opens in a position of the template and not in a new tab

I just wrotge a php file and I puted it in the file manager in the site so you can get to it with the url like this: http://www.eatwizard.com/project081113/ ... select.php
Now I created a link in the menu a the main page, and I wanted to deliver the page into the mainframe (to a position of the template), instead the link is being opened in a new tab, any ideas of how I can change it? thanks!!!!
You should use the "target" attribute of the tag.
Syntax :
<a target="_blank|_self|_parent|_top|framename">
In your case, specify the destination framename.
For instance :
<a target="my_rightupper_frame" href="http://....." >Here's a link</a>
See details : http://www.w3schools.com/tags/att_a_target.asp

Extract URL from document.write()

I have a simple html page that sources a javascript file. The javascript file's only purpose is to write the following...
document.write('<img src="http://www.location.com/image.png">');
Once the information has been written, im needing some javascript to extract the url and the image source and return the url and image locations alone.
Any help is appreciated. Thank you in advance!
Check out this answer which gives shows you how to do it with JQuery or just plain Javascript
UPDATE:
If you have the ability to modify the HTML, then why don't you put in a DOM element that you can hook on to right after where the image will be inserted? Then you can use the following JQuery:
var linkDest = $('#Anchor').prev().attr('href');
var imgSrc = $('#Anchor').prev().children().attr('src');
Which you can see in this JSFiddle example