File upload error in flask - file-upload

#app.route('/registerdriver', methods=['POST'])
def register_driver():
fname = request.form['fname']
lname = request.form['lname']
email = request.form['email']
mobno = request.form['mobno']
password = request.form['password']
file = request.files['driving_license']
file.filename = mobno+"_"+fname
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
Above is the code I used for saving the file. However the following error pops out while trying to save the file
flask.debughelpers.DebugFilesKeyError
flask.debughelpers.DebugFilesKeyError: You tried to access the file
"driving_license" in the request.files dictionary but it does not
exist. The mimetype for the request is
"application/x-www-form-urlencoded" instead of "multipart/form-data"
which means that no file contents were transmitted. To fix this error
you should provide enctype="multipart/form-data" in your form.
The browser instead transmitted some file names.
Can someone help me with this

In your html form tag include
<form action="/path" method="post" enctype="multipart/form-data">
</form>

Related

Apache tika: MSG remove extra line breaks in result string

I have msg file with body text:
<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;">
<div>Test message.</div>
<div> </div>
<div>More content here...</div>
<div> </div>
<div>Best regards,</div>
<div>Mr. Crowley</div></div></body></html>
I try to get content of the file above using Apache Tika...
final InputStream input = new FileInputStream("file.html");
final ContentHandler handler = new BodyContentHandler();
final Metadata metadata = new Metadata();
final HtmlParser htmlParser = new HtmlParser();
htmlParser.parse(input, handler, metadata, new ParseContext());
String plainText = handler.toString();
System.out.println(plainText);
...and all is fine except extra linebreaks:
Test message.
More content here...
Best regards,
Mr. Crowley
<and 3 empty lines here>
Is it possible to avoid this behavior? Is it possible to get more expected result?
Help me to fix this.

Upload image using lua

I have been trying simple image upload using lua and Openresty web framework. I found many solutions like
lua-resty-upload
lua-resty-post
Using lua-resty-post I got the form data now how do I upload it?
local resty_post = require 'resty.post'
local cjson = require 'cjson'
local post = resty_post:new()
local m = post:read()
ngx.say(cjson.encode(m))
As I'm new to lua I don't understand which one to use.
My requirement is very simple, I need a file attribute and want to upload on some place like in php move_uploaded_file. Is there any simple way to upload a file?
Found the solution. Using lua-resty-post.
upload.html
<form action="/uploadimage" method="post" enctype="multipart/form-data">
<input type="file" name="upload" accept="image/*">
<button>Upload</button>
</form>
nginx.conf
location /uploadimage {
content_by_lua_file image.lua;
}
image.lua
local resty_post = require "resty.post"
local post = resty_post:new({
path = "/my/path", -- path upload file will be saved
name = function(name, field) -- overide name with user defined function
return name.."_"..field
end
})
local m = post:read()

Upload a file in Classic ASP without another class completely custom

Yes I am using Classic ASP, not by choice I am supporting a Legacy application. Objective: I need to have a form page that submits to another .asp page that will upload the file and store it on the server in a certain directory such as "/uploads". I'm not real familiar with asp or asp.net so I am very new to this. I've created a test prototype:
Form page:
<!DOCTYPE html>
<head>
<title>Test upload</title>
</head>
<body>
<form action="process.asp" method="post" enctype="multipart/form-data">
<p>Filename: <input type="text" name="filename" size="50" /></p>
<p><input type="file" name="file" /><input type="submit" value="Upload file" /></p>
</form>
</body>
</html>
Processing page:
<%
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set tfolder = fs.GetSpecialFolder(2)
tname = fs.GetTempName
'Declare variables
Dim fileSize
Dim filename
Dim file
Dim fileType
Dim p
Dim newPath
'Assign variables
fileSize = Request.TotalBytes
fileName = Request.form("filename")
file = request.form("file")
fileType = fs.GetExtensionName(file)
fileOldPath = tfolder
newPath = Server.MapPath("/uploads/")
fs.MoveFile fileOrigPath, newPath
set fs = nothing
%>
The problem is that everytime I try to upload or run the script I get this error:
Microsoft VBScript runtime error '800a0035'
File not found
/tbird/fileUpload/process.asp, line 25
Obviously I'm not mapping correctly to the file and I think the major reason I am getting stuck is that in the first parameter of the MoveFile method I am not mapping to the file correctly. Can anyone tell me how I should be referencing the file or if I am doing it wrong?
Thanks in advance I would really appreciate the help I've searched all over and everything I find related to classic asp and uploading files are classes that you can purchase and I don't want to do that.
Have a look at a solution like Pure ASP Upload, it should help you. In classic ASP, you cannot directly access Request.Form when data is sent in multipart/form-data, so you have the choice of using a third party component like ASPUpload or a ASP class that does the work of parsing the request for you and exposing methods to save the file.
When moving files you must also specify the file names.
Change:
fs.MoveFile fileOrigPath, newPath
To:
fs.MoveFile fileOrigPath & fileName, newPath & fileName
Assuming "fileName" is the correct variable for the file name and not "file" variable above.

I need help in uploading a file to a folder on my server and then adding the file details to a database

I am trying to make a page for my site where users can upload a game, give it a name then the file gets uploaded to a folder on my site and the file name and game name get added to my sql database but it gives an error saying: "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" when i try to run it.
My code for this page is:
#{
var db= Database.Open("Games");
var sqlQ = "SELECT * FROM Games";
var data = db.Query(sqlQ);
}
#{
var fileName = "";
if (IsPost) {
var fileSavePath = "";
var uploadedFile = Request.Files[0];
fileName = Path.GetFileName(uploadedFile.FileName);
fileSavePath = Server.MapPath("~/App_Data/UploadedFiles/" +
fileName);
uploadedFile.SaveAs(fileSavePath);
}
var GameName="";
var GameGenre="";
var GameYear="";
if(IsPost){
GameName=Request["formName"];
var SQLINSERT = "INSERT INTO Games (Name, file_path) VALUES (#0, #1)";
db.Execute(SQLINSERT, GameName, fileName);
Response.Redirect("default.cshtml");
}
}
<h1 >Add a new game to the database</h1>
<form action="" method="post">
<p>Name:<input type="text" name="formName" /></p>
#FileUpload.GetHtml(
initialNumberOfFiles:1,
allowMoreFilesToBeAdded:false,
includeFormTag:true,
uploadText:"Add")
The error page says that the problem is with line 11 but i don't know what to change.
As Hightechrider has correctly said, your code should reside in Controllers, not in views. The exception you are getting is because Request.Files is an empty array, so when you're trying to access [0] element, you get an IndexWasOutOfRange error.
I'd recommend you reading the following article for uploading files in ASP.NET MVC. It presents an easier, more consistent and flexible model by putting the code in the controller action. Basically, all code boils down to this:
You create an upload form with submit button and file input.
You create an action that accepts HttpPostedFileBase variable.
You must set the proper enctype to your form if you want to upload files:
<form action="" method="post" enctype="multipart/form-data">

How to upload a file (via web) using Lua?

How do I upload a file, from a browser, using the Lua programming language?
I'm using the Orbit web framework
This sample comes straight from the orbit sample pages/test.op.
<form method="POST" enctype="multipart/form-data" action="test.op">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
$lua{[[
local f = web.input.file
upload = {}
if f then
local name = f.name
local bytes = f.contents
local dest = io.open(web.real_path .. "/" .. name, "wb")
if dest then
dest:write(bytes)
dest:close()
upload[1] = name
end
end
]]}
You can easily adapt this to a normal orbit post handler. You can also take a look at how I used it in my library project, but it's way more complicated than your typical usage I guess.