2

I'm trying to save the link to a local file in my model using a model form. I don't want to upload the selected file itself, I only want to save the fullpath to the file:

  1. Select a file via <input type="file"> on the rendered form view
  2. Submit the form via POST
  3. Dont upload the selected file, but save the fullpath of the file e.g. 'C:/myuploadedfile.pdf' in my model

What I tried so far, aresome hacks in form.clean() which didn't even worked.

Some suggestions how to accomplish such requirement?

Malte Jacobson
  • 215
  • 1
  • 11
  • Even if it were possible, which it isn't, what could be the use of storing a local file path on a user's computer? – Daniel Roseman Nov 15 '13 at 19:18
  • We use the application in our company, internal only. So the file path will point to a network drive which is assessable for the user. – Malte Jacobson Nov 15 '13 at 22:36

1 Answers1

2

First, you won't be able to get the full local path from html/javascript. This is due to browser security to prevent disclosure of such information. You'll get the file data and file name when you submit a file to a POST. You might be able to gather the information using Flash, Silverlight, Java, or any other plugin that gives you lower level access, however that's much more complicated than just form submission.

Second, suppose you just wanted to save the file name. You can just look at request.FILES['input_name'].name to get the name of the file and store it directly to a TextField.

Tim Edgar
  • 2,143
  • 14
  • 11
  • Thanks Tim. This is probably the first step. Second step would be to modify the `fileField`/`` widget in order to provide (1) the link to the local file and (2) to edit the local file link. I thought there might be a "copy-and-paste" solution for this. – Malte Jacobson Nov 15 '13 at 22:38
  • If you aren't storing a file, don't use a `FileField`. You aren't bound to use django forms. You can simply create your own html and process the response to meet your requirements. – Tim Edgar Nov 15 '13 at 23:04