Asked 4 years, 7 months ago. Active 1 year, 8 months ago. Viewed 9k times. Jens Testing man Testing man 10 10 silver badges 25 25 bronze badges. Add a comment. Active Oldest Votes. You need to read that file. Serve it using HttpResponse along with proper content type. Farid Chowdhury Farid Chowdhury 1, 16 16 silver badges 16 16 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.
Email Required, but never shown. This is also the basis for ImageField , which you probably use for things such as user avatars or other user-uploaded images, even if your use case doesn't involve managing other generic uploaded files.
A common UI pattern these days is to allow the user to either upload the file, import from a connected cloud storage account Google Drive, Dropbox and so on , or paste a link to the file located anywhere on the Internet. The user might also be signing up via a social provider, with your application getting the URL to the user's profile image in the account data for the newly created user. Importing files from cloud storage will use their API directly and differs slightly from service to service.
In this article, we'll only focus on the last case: when the user or a social auth provider supplies an URL to the file and we want to import it. What we want to do is download the file from the URL on a temporary location on the server, save it to the File field which will potentially have the effect of uploading it somewhere else, for example S3, if youse such a service for file storage , and remove any temporary files left.
This is common enough to warrant extracting into own function, yet simple enough that it doesn't really need to be packaged and used as a separate standalone package. Note: examples here assume Python 3. All the same functionality is available in Python 2, just the import paths for some functions are different. The simplest way is to use urlretrieve function from the Python standard library.
A naive approach could work like this:. File type. Python version. How to get Django. Django is available open-source under the BSD license. We recommend using the latest version of Python 3. Django's own file upload and storage API alone is too limited because 1 it doesn't provide a mechanism for file downloads and 2 it can only handle direct uploads which eat a lot of resources and aren't compatible with cloud services like the App Engine Blobstore or asynchronous Amazon S3 uploads where the file isn't piped through Django.
So guys there are many ways to download files using python.
0コメント