0

I am fairly new to Django. I've created a simple template page to test a service I'm writing. The service does two things - accepts a file through POST, and accepts a string path to the file through POST. This is more of a template question.

I've figured out how to send the file content itself, but sending the file path WITHOUT the file data from the template page is something I haven't been able to wrap my mind around. The operation of the template page's file path submission should be the same as the file submission: the user picks a file and hits submit, but only the text path should be submitted, not the file data.

Any help is appreciated!

<h1>File Upload</h1>
<form action="upload/file/" method="post" enctype="multipart/form-data">
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
<p><input type="submit" value="Upload" />
</form>
<br>
<h1>Path Upload</h1>
<form action="upload/path/" method="post" enctype="application/x-www-form-urlencoded">
<p>{{ form.non_field_errors }}</p>
<p>{{ form.docfile.label_tag }} {{ form.docfile.help_text }}</p>
<p>
{{ form.docfile.errors }}
{{ form.docfile }}
<p><input type="submit" value="Upload" />
</form>
</body>
</html>

And if anyone wants to see if, the forms.py I'm using.

from django import forms

class UploadFileForm(forms.Form):
docfile = forms.FileField(
    label='Select a file',
    help_text='max. 42 megabytes')
AKS
  • 29
  • 6

1 Answers1

0

docfile is a FileField, which outputs an <input type="file"> the user's file system path to the file is never submitted from a form. Only the name and extension of the file. If you really need the file's location on the user's computer, (No idea why you would need that) my suggestion would be to just have another form with a regular <input type="text"> that the user would just type the text in. I know its not elegant, but as far as I know it is the only way. A better suggestion would be to rethink wether or not you actually need the user's path.

See this question. It is basically a security reason that you cannot get the file path automatically. How to get full path of selected file on change of <input type=‘file’> using javascript, jquery-ajax?

Community
  • 1
  • 1
jproffitt
  • 5,677
  • 26
  • 41
  • It's because the requirements are unclear as to if the service accepts a file or a path, so I decided to do both to cover myself. I'll create another form with an input, and try that.. Thanks for replying – AKS Oct 11 '13 at 17:59