My Objective: I am trying to upload event log file (.evtx), convert it in the backend using LogParser tool into csv, and visualize the data afterward. The Logparser tool will be run by command line using os.system(cmdline_string). Basically I am trying to parse the uploaded file without saving the temporary uploaded file to the file system.
My Problem: I am parsing the event log using Log Parser's command line. I cannot access the temporary uploaded file in appdata\local\temp using log parser's command line.
Code:
uploadfile.html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="myfile">
<br>
<button type="submit">Upload</button>
</form>
views.py
import os
import pandas as pd
def uploadfile():
if request.method == 'POST' and 'myfile' in request.FILES:
myfile = request.FILES['myfile']
filename, fileext = os.path.splitext(myfile.name)
if fileext == '.evtx':
cmdstring = "logparser " + \
"\"SELECT * INTO " + "output.csv" + \
" FROM " + myfile.temporary_file_path() + "\" " + \
"-i:EVT -o:CSV"
os.system(cmdstring)
# This will output error:
# Cannot open <from-entity>: Error opening event log /path/to/uploadedfiles:
# The process cannot access the file because it is being used by another process
I am guessing that probably the temporary file is being accessed by django thus logparser cannot access the file. When I tried to save the file into filesystem, logparser is able to access and parse the file. When I simply access and parse any event log data in AppData\Local\Temp directory, it is also manage to do it.
How to let another application access temp uploaded file in django?
My Environment: Windows 10, Python 3.6.1. Django 1.11.4, Log Parser 2.2
Related
I'm uploading multiple files to flask using a form, I'm getting the file objects in the flask backend without a problem but the issue is I want to read the PDF files to extract text from them. I can't do it on the file objects I received from the form, another method I thought of was saving the file in the local storage then read them again when I did that using file.save(path, filename) it created an empty text file with the name - filename.pdf
app=Flask(__name__)
#app.route('/')
def index():
return '''
<form method='POST' action='/saveData'>
<input type='file' name='testReport'>
<input type='submit'>
</form>
'''
#app.route('/saveData', methods=['POST'])
def saveData():
if 'testReport' in request.files:
testReport= request.files['testReport']
#This isn't working, a text file is saved with the same name ,ending in pdf
testReport.save(os.path.join(app.config['UPLOAD_FOLDER'], testReport.filename))
return f'<h1>File saved {testReport.filename}</h1>'
else:
return 'Not done'
How do we operate on PDF files after uploading them to flask ?
How do we operate on PDF files after uploading them to flask ?
You should treat them just like normal PDF files - if they were uploaded via Flask application or gathered using other method is irrelevant here. As you
want to read the PDF files to extract text from them.
you should use PDF text-extraction tool, for example pdfminer.six, as this is external module you need to install it first: pip install pdfminer.six
You can directly follow the flask own way as mentioned [here]
This easily works with pdfs. Just don't forget to include your extension in ALLOWED_EXTENSIONS
Hi so basically I have a form that uploads a file
<input class="btn btn-light" type="file" id="file" name="h" accept=".py">
I'm using bottle, so essentially I want to get the contents of the file but not have to save it locally. Using with open requires a path which I don't have as it is not saved locally.
f = request.POST['file']
I get the file using the above.
Is there any way I could upload the file without having to save it locally?
Any help would be much appreciated, I just need the name of the file and the contents.
No need to save the file (or to use BytesIO as suggested in a comment). You can simply read its contents:
from bottle import Bottle, request
app = Bottle()
#app.post("/upload")
def upload():
f = request.POST["file_name"]
text = f.file.read()
return f"Read {len(text)} bytes from the uploaded file.\n"
app.run()
Output:
$ echo "This is the file to upload." > file.txt
$ curl http://127.0.0.1:8080/upload -F file_name=#file.txt
Read 28 bytes from the uploaded file.
I have a web application written in Python and Django framework that allows uploading a file. The uploaded file path is shown in a textbox. When I browse and select a file it is shown as c:\fakepath\Sample.docx in the textbox and when I click the upload button the file upload happens hassle-free.
But if I provide the absolute path in the textbox as C:\xxx\xxx\xxx\sample.docx and click the upload button, the file doesn't upload.
Html code to create the upload in a textbox:
<input id="txt" type = "text" value = "Choose File" size="40" align="center"
onclick ="javascript:document.getElementById('file').click();">
<input id = "file" type="file" style='visibility: hidden;' name="file1"
onchange="ChangeText(this, 'txt'); Filevalidation()">
Python code when I try to extract the file uploaded
file = request.FILES['file1']
Error while giving the absolute path:
raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'file1'
That happens because you're not sending any actual file in the request by just typing the file path, so, that 'file1' key is not filled in the request.FILE dictionary. So, you can do:
file = request.FILES.get('file1', '')
This will handle the case when that key does not exist and give a default value (if not passed, default would be None).
But still, doing this just avoid getting the error, but the file won't be sent anyways. You can't send a file to the server by just writing the path where it is stored.
HTML form :
<input type="file" name="upload"/>
file = request.files.get(upload)
Lets say i have apple.xls in this path : c:/desktop/tempfolder/Apple.xls
step 1: I am clicking browse button to upload the file
name,ext= os.path.splittext(upload.filename)
It gives filename and its extension
I need file path mentioned in the input box like c:/desktop/tempfolder/Apple.xls
When i tried using my project name is Appbuilder
pat=os.path.abspath(upload.filename)
It gives c:/desktop/tempfolder/(applicationname:Appbuilder)/Apple.xls
I'm trying to work file upload example in this website https://www.tutorialspoint.com/python/python_cgi_programming.htm
I have written a .py file. When i run this code, following error is occurs;
Traceback (most recent call last):
File "C:/xampp/cgi-bin/file_upload.py", line 9, in <module>
fileitem = form['filename']
File "C:\Python3\lib\cgi.py", line 604, in __getitem__
raise KeyError(key)
KeyError: 'filename'
And my .py file is below:
#!C:/Python3/python.exe
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
open('F:/gelen/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
print ("""\
Content-Type: text/html\n
<html>
<body>
<form enctype="multipart/form-data"
action="/cgi-bin/file_upload.py" method="post">
<p>File: <input type="file" name="filename" /></p>
<p><input type="submit" value="Upload" /></p>
</form>
<p>%s</p>
</body>
</html>
""" % (message,))
How Can i solve this problem and why program doesn't see filename i don't understand
If the directory where the script is running is /path/to/dir then the /path/to/dir/files directory must exist. If it does not it will fail.
and also to upload a file the HTML form must have the enctype attribute set to multipart/form-data.
Perhaps it is late for the person who asked, but I came across a similar problem. The following is what worked for me. As your error message shows the problem is coming from the line. fileitem = form['filename'] We can run the file in the browser as http://localhost:xxxx/file_upload.py What you'll see is the 'browse' button and the 'upload' button. Unless you browse and load some file the 'form' object won't be populated. It wouldn't contain the key 'filename', yet. We get the keyerror. So we need to put it inside an if statement. I also found some formatting error with the html part of the code. I slightly edited the code which is pasted below, runs well on Linux.
#!/usr/bin/python3
import cgi, sys, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
print('Content-type: text/html')
sys.path.insert(0, os.getcwd())
message = None
# Test if the file is loaded for the upload
if 'filename' in form:
fileitem = form['filename']
fn = os.path.basename(fileitem.filename)
open('/home/jk/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
replyhtml = """
<html>
<body>
<form enctype="multipart/form-data" action="/cgi-bin/file_upload.py" method="post">
<p>File: <input type="file" name="filename" /></p>
<p><input type="submit" value="Upload" name=action/></p>
</form>
<p>%s</p>
</body>
</html>
"""
print(replyhtml % message)
I believe you have your server running in the current working directory. And the .py file need to be in the cgi-bin folder. A simple http server script:
import os
from http.server import HTTPServer, CGIHTTPRequestHandler
servaddr = ("localhost", 8888)
#http.server.SimpleHTTPRequestHandler(request, client_address, server)
server = HTTPServer(servaddr, CGIHTTPRequestHandler)
server.serve_forever()
References:
Programming Python, Mark Lutz, 4th edition, Chapter1
https://www.youtube.com/watch?v=oQ9FwkhUN1s
http://cgi.tutorial.codepoint.net/file-upload
Also if you are uploading images to an ec2 linux server then the folder you want to upload your files into must have the permissions drwxrwxrwt.
chmod 777 -R myfolder
chmod o+t -R myfolder
this worked for me
Referred from here