How to download auto generated file in Django? - python

I am created a basic ocr system. When user uploads a PDF file to system ocr read it and created a xlsm file with the same name. I am using remote control with Azure (Windows). I created a download button. When I click it, downloads the file with same name, so it should work but appears a error.
How can I fix this problem?
<a href= "{% static path %}" class="btn btn-outline-primary btn-sm" download>Download Report File</a>
in my views:
name = pdf.pdf.name.replace(".xlsm", "").replace(".pdf", "")
path = "C:/user/ocr/" + name + ".xlsm"
Note: Uploaded and created files are stored outside of the project file.

please get more error info from django server. and list more code from my views.
Do you want to change file filename?

Related

django static file not loading after updating it on production

On a django web app. I have a script which runs when i goes to myapp.com/update url it fetches data using bs4 library and convert the data into a pandas dataframe.
To show that data I used pd.to_html to convert it in HTML table format and stores that HTML file in static folder and then load it in my index.html(which present in templates folder using jquery).
The HTML loads fine but after the I visit the update url it stops showing the updated HTML file (pandas df one). The updated HTML file (pandas df one) is still available on azure panel. The site is not loading it anymore but before update it was loading perfectly.
How do I display the updated HTML file?
--script in my views.py (for myapp.com/update url)--
text_file2 = open("staticfiles/album.html", "w")
text_file2.write(updatedData)
text_file2.close()
This is how I'm adding that HTML file to my main index.html file
(this loads the file perfectly but after update url it stops loading it)
<script>
$(function () {
$("#includedAlbums").load("../static/album.html");
});
</script>

Django : upload direct folder to input type file instead selecting multiple files

How can we upload direct folder to input type file ?
I mean user can upload direct folder of images or files instead of selecting multiple files?
that's a javascript question
<div class="picker"><input type="file" id="picker" name="fileList" webkitdirectory multiple></div>
let picker = document.getElementById('picker');
picker.addEventListener('change', e => {
for (let file of Array.from(e.target.files)) {
//now append files to formdata
};
});
found this on previous question Upload folder and all its content in JavaScript

How download files when href values in their WebElements are a path to the same folder?

I try to automate the downloading of couples of text files. Operating manually, they can be downloaded by clicking on the buttons defined by the 2 following elements.
<a class="btn ...." href="/..../...../....../..../download" data-value="ext1">Download (.ext1) file</a>
<a class="btn ...." href="/..../...../....../..../download" data-value="ext2">Download (.ext2) file</a>
Please note that the two href values are the same and it is a path to a folder.
Using Selenium, I find the 2 FirefoxWebElements by
elem1=driver.find_elements_by_link_text("Download (.ext1) file")
elem2=driver.find_elements_by_link_text("Download (.ext2) file")
I can download the two files by
elem1.send_keys(Keys.ENTER) and
elem2.send_keys(Keys.ENTER),
then I have to find the two files in the download folder and I have to move them to the destination folder.
Note that I do not know the name of these files.
I would prefer to do it by requests to have them directly at their destination folder with the appropriate names.
url1=elem1.get_attribute('href')
r = requests.get(url1, allow_redirects=True)
open(destination1, 'wb').write(r.content)
and
url2=elem2.get_attribute('href')
r = requests.get(url2, allow_redirects=True)
open(destination2, 'wb').write(r.content)
But I get the same file (the first one) in both the cases since href value is the same.
Would it be possible to download the two files directly to their destination folder? (may be additional parameters of requests.get or other ways?)
You can try tracking the network-tab on your browser when clicking the download-button and see what kind of request is sent. Maybe the request has "ext1" or "ext2" in the payload or something to separate between the files. Sending payload for GET request in python

How to get full path location of file in HTML

I am trying to return file path on click of Submit button in HTML. File path has to be sent to python variable. Requirement is user can select file and if this file location is different than pre-configured file location then new file location should be updated in python. But only file name is being returned.
<label for="excel">Choose file location: </label>
<input type="file" id="excel" name="excel" accept=".xlsx, .xlx, .doc, .pdf">
example.py
file = request.form.get("excel")
if 'Update File Location' in action:
if file:
c.update_file_location(file)
As of now, only selected file name is returned in 'file'. But I want to get file path also along with filename.
Is there any way to achieve it?
Thanks in advance!

Uploading file using python mechanize

I am on a windows 10 laptop.
When i manually open submit.html on my local computer click and browse to namo.jpg namo.png and then submit, i get the website processing my image and return with result file within 15 seconds.
But I can't seem to get it to do the same using Python mechanize, when it run the script, the mechanize_results.html file keeps returning too quickly and telling me in their page that "Uploaded file is not a valid image. Only JPG, PNG and GIF files are allowed.. "
Not sure what i have to change to get the site to recognize my file submitted by my python mechanize script as an image file.
my submit.html file has this
<form name="myform" id="myform" action="http://deepdreamgenerator.com/upload-im" enctype="multipart/form-data" method="POST" id="upload-form">
<input type="hidden" name="_token" value="pfC1a6HGVdbWO7mCmKVkqVinCkSYOKkQxXZV9NY1">
<input type="file" name="file" id="upload"/>
<input type="submit" />
</form>
my python mechanize script has this
import mechanize
filename = 'C:/Users/tintran/Desktop/namo.png'
url = "file:///C:/Users/tintran/Desktop/submit.html"
br = mechanize.Browser()
br.set_handle_robots(False) # ignore robots
br.open(url)
br.select_form('myform')
br.set_all_readonly(False)
br.form.add_file(open(filename,'r'))
res = br.submit()
content = res.read()
with open("mechanize_results.html", "w") as f:
f.write(content)
https://docs.python.org/2/library/functions.html#open
If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.
It's all about Windows here. So just use 'rb' for opening PNG file.

Categories

Resources