I want to rename an image that is uploaded with a Modelform and I want to do this in the view because the path depends on the user who uploaded it.
So far i have this:
if request.method == 'POST':
form = AddImageForm(request.POST, request.FILES)
if form.is_valid():
new_image = form.save(commit=false)
But how do I get the image, rename it and save it. (Maybe I will also do some additional proccessing like resizing, etc.)
I'm quite new to python an Django and I have no clue how to manipulate files.
Best Jacques
the os module lets you interact with the filesystem, and the data in the all the uploaded files is available in the dict request.FILES.
Django file upload docs
so maybe you would do request.files.items()[0][1]
Related
I'm using the image upload script with a backend URL to rename uploaded images stored on the server. It seems to work well with both file uploads and also drag-and-drop.
Without that backend handling you can paste images directly into the editor, but they get saved as base64 with the post in the db itself which is not great for a lot of reasons.
If I use the image upload script as I am now, it seems it blocks the pasting of images directly into the editor. That's safest, I guess, but I was wondering if there was a way of allowing it, but treating the pasted image like a drag-and-drop image, for example, so we could have the convenience of pasting images but still have them saved with unique filenames on the server instead of being embedded as base64 with the rest of the source of the post?
Thanks.
I'm developing an application with Unity in VR, I need to read a filename, which inserts the user, to be uploaded within the application and other 2-3 options that always the user could choose. I had thought of an initial menu in which to put that data but it is not practical for a VR solution. So I had thought of creating a ' setting ' file where the user, before starting the application, inserts the options, the name and the path of the file from which to load the data. But I do not know how to read a file that is outside the resources of the Unity project. Also how can I create this file setting so that it is always in the same location after I Build the project and is visible within the EXE folder? Would anyone know how to help me? Or maybe give me some advice on how to proceed? Thanks in advance.
Welcome to the stack!
I suppose you are creating a desktop-based game? For your case I suggest using the StreamingAsset folder to store the config file. When you build the game it will be placed in <BuildFolder>/<GameName>_Data/StreamingAssets.
For loading a file, you can use any of C#'s many different System.IO classes, a simple way to read file's content as text could be something like this:
var path = Application.dataPath + "/StreamingAssets/settings.txt";
using (StreamReader reader = new StreamReader(path))
{
try
{
var fileContent = reader.ReadToEnd();
/* do something with the content here */
}
catch (System.Exception e)
{
Debug.Log(e);
}
}
Another approach for your situation, which I think is more user-friendly, is to use something like FileBrowser asset which allow user to choose a file in a keyboard-free manner. I don't know if it works with VR though.
I am making a app in which I am retrieving data from Database in List View.
I want to have different picture with each data I retrieve from database in list View.
What I am trying to do is to save my pictures in drawable folder of my app and want to save image path in database and retrieve my image from saved path.
I searched lot on internet but couldn't get appropriate solution.
Somebody please show me appropriate code.
Don't know if you already have solved this problem,
but after looking for it myself i found a solution.
what i did was use this.
i stored my images in a subfolder in "Assets" and this works like a charm,
don't know how many images it can hold in total, but i'll find out eventually i guess xD
I have a form in which you enter some data and you are able to attach files. All I wanna do is have a Browse button so i can select the file and store that file url in some var, so later when i want to submit the form...y can read that var and upload the file using its url. Im using Scala Lift. I search for examples in the web but couldnt find anything helpful. I only want to select the file and store the file url. Can someone help me? Thanks!
You can't store path to file on client's drive. Even if you get path like this, you can't upload file that is not manually selected by client.
I suggest doing AJAX based client.
I have an app I'm designing that will allow for lots of PDF viewing. There are a lot of different languages available, and so if I were to include all of them in the app, it would be like 100+ mb in size which just won't fly.
So I'm thinking that I am going to put the pdf's on my server, and access them with a direct download link like this:
http://mysite.com/pdfs/thepdf.pdf
Which will return the exact pdf I want. So I'm wondering how I can go about accessing these resources as I download them on the fly?
I imagine I need to save the pdf's to the app resources folder? And then when a tableView row for the pdf is selected, I check if the pdf is in the resources folder (how do I do that?), and if not, pull it down off the server, and load it into my view?
I think I have an okay idea of what I need to do, just not very clear on the code to do it. Can anybody post the code for accessing the resources folder (if that's actually what I need to be doing), and maybe the code for how to check if something is in the resources folder?
Thanks!
Have you considered using a UIWebView to view the PDF instead of downloading and loading it yourself? UIWebView should take care of caching, so you won't have to worry about that.
Assuming that a UIWebView won't work, to download PDFs and see if they exist, you need to store it in the Documents folder. The resources folder cannot be altered after you submit your app to Apple, but the Documents folder in your app is completely fine. To access it, I would actually recommend ConciseKit, which can be found on GitHub. It gives you a helper method to access your app's document directory. The helper method is
[$ documentPath];
Then you can get the path for a file by doing
[[$ documentPath] stringByAppendingPathComponent:#"file.pdf"];
So that is how you get a path to a file, to check if it exists, you want to use NSFileManager.
[[NSFileManager defaultManager] fileExistsAtPath:#"path from above"];