fakepath for uploading file html-python - python

I have this file input:
<input type="file" name="file" id="file_id" />
and I want to upload a file from local machine (C:\my_file.xls) to the server, but the problem is when I want to upload the file.
It has writen a fakepath to the file and I have got this message when uploading:
No such file or directory: u'C:\\fakepath\\my_file.xls'
I knew that it has relation with browser security feature, and I have tried to make some solutions for that like creating a folder with the name fakepath or delete this word from the given path...
Is there an other (elegant or efficient) way to get file's local full path in the input (the real path) for the file ?

You cannot actually get the file path from the client side. That just wont work.
You have to change how you think - the client is completely separate from the server. What you do on one is only partially related to the other (and good thing, too!)
What you need to do is decide where on your server you want to store files. And then put them there. If you want to be able to download the files that have been uploaded, you have to decide who can access the files, how you're going to list them, etc.
But these should be completely unrelated to the information you get from the client - the only thing you should get from the client is the data contained in the file (and maybe a filename).

Related

Using temporary files and folders in Web2py app

I am relatively new to web development and very new to using Web2py. The application I am currently working on is intended to take in a CSV upload from a user, then generate a PDF file based on the contents of the CSV, then allow the user to download that PDF. As part of this process I need to generate and access several intermediate files that are specific to each individual user (these files would be images, other pdfs, and some text files). I don't need to store these files in a database since they can be deleted after the session ends, but I am not sure the best way or place to store these files and keep them separate based on each session. I thought that maybe the subfolders in the sessions folder would make sense, but I do not know how to dynamically get the path to the correct folder for the current session. Any suggestions pointing me in the right direction are appreciated!
I was having this error "TypeError: expected string or Unicode object, NoneType found" and I had to store just a link in the session to the uploaded document in the db or maybe the upload folder in your case. I would store it to upload to proceed normally, and then clear out the values and the file if not 'approved'?
If the information is not confidential in similar circumstances, I directly write the temporary files under /tmp.

How to get around Heroku resetting files?

I have to python files that create and read text from a .txt file, in order for them to work they need to know the info inside of the .txt file.
In heroku I have a scheduler that runs one file, then the other. The big problem is that the files are reset every time to their state from the original repo. How can I get around this?
Heroku does not offer a persistent file system. You will need to store them in another service (like S3), or depending on what the contents of your files are, redesign to write and read from a database instead.

How to pass directory path from a template to a view?

I'm pretty new to Python\Django.
I'm trying to make a local Netflix-like Library for my Movie\TV shows collection.
The general idea is that the user chooses a media folder, the server side runs on the files in the folder adds them to the database and then the user can search for the items and play them back in the GUI.
The first snag I ran into is getting the folder path from the user without actually uploading any files. After doing some searching online I found this :
<input type="file" id="file_input" webkitdirectory="" directory="">
This HTML code allows the user to choose a folder and iterates through all the files inside, however, I don't know how can I pass this information to views.py so that I could run logic on the input.
Does anyone know how this could be accomplished ?
For security reasons, browsers do not allow to get folder path (Stack Overflow). Since you do not want the user to upload files, the possible solution would be to explicitly mention the folder path in <input type="text">. The simplest solution would be python –m SimpleHTTPServer (source, docs). You may be also interested in this Django app.

Django Upload file get full name and file path

I'm tinkering around with cloud storage api's and need help getting the full file name from an upload form on Django. I currently can let the user choose a file, "file.txt" from any directory, and can get the name through
for file in request.FILES.getlist('file'):
print file.name `
However I want the full file path, something more like 'home/user/documents/file.txt'
Is this possible and how would I get the full name? I'm not looking to actually upload the file, just get the full path so that I can utilize dropbox/google drive api.
For reference here is my form:
class UploadFileForm(forms.Form):
folder_name = forms.CharField(max_length = 300)
title = forms.CharField(max_length=50)
file = forms.FileField(label='Select a file')
Thanks in advance.
Browsers do not tell what directory the files come from.
They do not even give that information to the scripts on the page.
When a user uploads a file, you may know:
Its basename.
Its size.
Its type (usually guessed from extension).
Its modification time.
This is all you will get, regardless of whether you access the information straight from the browser's <input> element, or wait for it to POST it.
You may also turn this answer the other way around: if you really need to be able to post the full path of the file, you need to develop a client-side application that will send it. It could be a standalone executable, a browser addon/app, or a Java applet, whatever as long as it runs outside of the webpage sandbox.
I do not use dropbox, but I believe you need to download and install some additional software to use it. That's how it would access the full path of your files.

Is there a django app that provides a file chooser for the files on the server?

I need a component that's a browser-based file browser, and I expect some django app to currently provide this. Is there such a thing?
The full story:
I'm building a django app that is used for testing. I want to use it to serve files (and strings, and etc.) and attach custom headers to it.
Currently, I have a model FileSource which has a single file_path field, which is of type django.db.models.FileField.
When creating a FileSource from the admin, the user has a nice file upload dialog, and when saving, the file he chose, is saved on the server (in a really weird location, inside the directory where django is installed, or something weird like that, because i didn't customize the storage, nor will it help me in any way)
My problem: I only want to use the file dialog for the user to select a full path on the server. The file that the user chose must be only referenced, not copied (like currently), and it must reside on the server.
The server must thus be able to list the files it has, so i basically need a little browser-based file-browser.
At that point, I expect to be able to save a full path in my DB, and then I'll be able to access that file and serve it (together with whatever custom headers the user will chose from my app).
Currently, as you might know, the browsers always lie about the full path of the file. Chromium appends "C:\fakepath" to the file name, so I need support of the backend to accomplish this.
Also, I checked out django-filebrowser and django-filer and from what I understood, they weren't built for this. If I'm wrong, a little assistence in configuring them would be awesome.
You can use a FilePathField for that. It won't upload a file, but rather allow you to choose a pre-existing file. A caveat is that you can only use one directory. If you need multiple directories, then you'd need do go with something like django-filer.

Categories

Resources