React On Click Prevent Download but show excel - python

I have a link from microsoft one drive which is generated when we upload file pythonically. I have linked this link to a button whichis supposed to show the file but not download however on click it is donloading the file so i neeed help regarding how to prevent download but show the excel file linked. Below is react code I have been using
function Files(props){
let getFileOnClick=(fileAddress)=>{
window.location.href=fileAddress
}
return(
<button style={{float:'right'}} onClick = {()=>getFileOnClick(props.link)} className="btn btn-primary">
Get File
</button>
)
}

Related

Html form open's file in text insted of showing output from python code

This form shuld show the output on current page
<div class="form">
<form action="QA.py" method="POST" onsubmit="return false; >
<label for="form-search"></label>
<input type="text" id="form-search" placeholder="Search code ">
<!-- BUTTONS -->
<input type="submit" value="code Search" id="code_search" onclick = "processQuery()">
</form>
<p>Answer: </p>
<p id = 'output'></p>
</div>
Insted it just opens this code given billow
def main():
print("Hello,")
IN = input("query ")
QT = f"Q: {IN} ? A:"
response = openai.Completion.create(
model="text-davinci-003",
prompt=QT,
temperature=0,
max_tokens=64,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["\n\n"]
)
user will input query in form-search on Html page
after clicking submit query shuld go to QA.py Python code
in python code input quary shud be submited to "IN"
after python Proveieds "A:" output shuld seen in 'output
Insted it just opens QA.py
You are missing a quotation mark on the form OnSubmit. The resulting javascript error allows the form to submit to the specified action which is your python file.
The Python file will only execute if run on a webserver that is properly configured. It may be that your web server is not configured for Python, or that you are running the html as a local file (ie from disk, not via a web server), and thus your Python file gets served as a file rather than executed.
It would appear you are trying to run a submit through javascript on a button. In which case the form action isn't required. Instead your processQuery() function which you haven't shared needs to specify which file to exectue.

Embeded pdf File is not displaying in Django

I was trying to display photos and pdf files in a django project using <embed> but pdf file is not displaying
Here is what i was doing
This is view function
#login_required
def view_documents(request):
students = Student.objects.filter(user=request.user)
return render(request, 'student/view_documents.html',{'students':students})
and then I used for tag in template
{% for student in students %}
#lines of code
{% endfor %}
and to display the pdf file i have used
<embed src="{{student.adhar_card_pdf.url}}" width="800px" height="400px" type="application/pdf"/>
but it was showing some kind of error like localhost refused to connect
i also used iframe
<iframe src="{{student.adhar_card_pdf.url}}" style="width:718px; height:700px;" frameborder="0"></iframe>
But image is displaying in the browser using same code without any error
<embed src="{{student.photo_jpg.url}}" width="230px" height="250px" />
Why pdf file is not displaying ?? Please help me
Thanx in advance.
Edited: In console it is showing -- Refused to display '' in a frame because it set 'X-Frame-Options' to 'deny'.
This is how it is displaying pdf
X_FRAME_OPTIONS = 'SAMEORIGIN'
Insert the above line in settings.py just before the MIDDLEWARE section
and for the file path in embed tag write path as
{{student.file.get.photo_jpg.url}}
And then give a hard reset in Chrome or any browser you are using.
It worked for me I hope it works for you as well.

Uploading files with Python Selenium without an 'input' html

I want to upload a file for Python Selenium however, the send_keys method (sending the keys with the directory of your file) does not work, I get a 'Message: element not interactable'. This is due to the fact that my upload button does not contain an 'input' on its html code. The code I currently have allows you to click on the 'upload files' button which then opens the little Mac Os window. After that, I have no idea what I can do. It is quite obvious that the send_keys method does not work.
The website I'm using also allows drag and drop, however I have been told that the drag and drop for Selenium does not work.
I was reading a lot about it but I couldn't find a solution specific to Python. A lot of the solutions referred to Java but I couldn't find any for Python. Anything I could do?
Here is the code I have:
button8 = driver.find_element_by_xpath("//[contains(#id,'FileName') and contains(#class,'x-btn')]")
button8.click()
time.sleep(1)
button9 = driver.find_element_by_xpath("//[contains(#id,'ext-comp-1056') and contains(#class,'x-btn')]")
button9.click()
button9.send_keys('/Users/sebastiendupont/Desktop/Screen Shot 2019-08-26 at 4.12.03 PM')
Here is the HTML code of the 'upload' button, I replaced the < with ( and > with ), because otherwise StackOverflow wouldn't display the html code
(table id="ext-comp-1056" cellspacing="0" class="x-btn btn-right upload-files x-btn-noicon" style="width: 100%; position: relative; z-index: 1;")(tbody class="x-btn-small x-btn-icon-small-left")(tr)(td class="x-btn-tl")(i) (/i)(/td)(td class="x-btn-tc")(/td)(td class="x-btn-tr")(i) (/i)(/td)(/tr)(tr)(td class="x-btn-ml")(i) (/i)(/td)(td class="x-btn-mc")(em class=" x-unselectable" unselectable="on")(button type="button" id="ext-gen179" class=" x-btn-text")+ Upload files...(/button)(/em)(/td)(td class="x-btn-mr")(i) (/i)(/td)(/tr)(tr)(td class="x-btn-bl")(i) (/i)(/td)(td class="x-btn-bc")(/td)(td class="x-btn-br")(i) (/i)(/td)(/tr)(/tbody)(/table)(
The codes sends the following classic message:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Please advise that I am very beginner at Python and Selenium but I have used complex codes.
Thank you!
You can use external tool AutoIT. You have to write script in AutoIT editor first and then compile it so it will generate .exe file.
After that you have to execute that .exe file in python using below code
import subprocess
subprocess.Popen(<path to AutoIT .exe file>)

Using bottle to download a file?

In index.html I got the following button:
<button id="btn_file" type="button" class="btn btn-default" onclick="getFile()"><span class="glyphicon glyphicon-file"></span></button>
In my javascript, picoreflow.js, I have this function:
function getFile()
{
var cmd = {"cmd":"DL"};
ws_control.send(JSON.stringify(cmd));
}
And in my picoreflowd.py, this is located in handle_control():
elif msgdict.get("cmd") == "DL":
log.info("Download command received")
static_file("downloadMe.txt", root='/home/pi/V2.3_Test/', download = "downloadMe.txt")
This is my github directory, which has the complete code: https://github.com/Ritzerk/V2.3_Test
I'm trying to work on open-source software, but I don't know anything about web-server. I tried to use static_file like suggested here: Binary file download
But its hard to know what I am doing wrong, since I have no website experience...
What's wrong with the way I am trying to download downloadMe.txt? The button doesn't even display on the website when I load it.
The file is located in the given directory. The V2.3_Test is located in /home/pi/.

Play a downloaded video with flask [duplicate]

This question already has an answer here:
Can't play HTML5 video using Flask
(1 answer)
Closed 7 years ago.
I have a simple flask server. I downloaded, using pafy, a video from a youtube link provided by the user.
#app.route('/')
def download():
return render_template('basic.html')
The basic.html template has a form that submits an action to download:
<form action="download_vid" method="post">
Link: <input type="text" name="download_path"><br>
<input type="submit" value="Submit">
</form>
I have another end point, /download_vid that looks like this.
#app.route('/download_vid', methods=['POST'])
def download_vid():
url = request.form['download_path']
v = pafy.new(url)
s = v.allstreams[len(v.allstreams)-1]
filename = s.download("static/test.mp4")
return redirect(url_for('done'))
The desired link is indeed downloaded as a .mp4 file in my static folder. I can watch it and I can also use it as a source for a tag in an HTML file, if I open it locally.
#app.route('/done')
def done():
return app.send_static_file('test.mp4')
From what I understand, 'send_static_file' serves files from the static directory. However, I get a 404 error when I run the server, even though the video is clearly there.
I have also tried a different version for done():
#app.route('/done')
def done():
return return render_template('vid.html')
Here, vid.html resides in templates and has a hard coded path to static/test.mp4. It is loaded after the download is complete. I do not have a 404 error in this case, but the tag don't do anything, it's just gray. If I open vid.html locally (double click on it), it works, it shows the video.
Can you please help me understand what is going on?
What I want to achieve is this:
Take an input from the user [ Done ]
Use that input to download a video [ Done ]
Serve that video back to the user [ ??? ]
I think you have something going on with file paths or file permissions.
Is the video being downloaded into static directory?
Is the static directory in the same directory, along with your main.py file?
Does your flask app have permissions to read the file?
I think the reason your file did not load in html template is because you referenced it as static/test.mp4 from an url - /done which translates the video path to be /done/static/test.mp4.
Instead of trying to push the file using Flask, you can redirect to the actual media file.
#app.route('/done')
def done():
return redirect('/static/test.mp4')

Categories