0

I am uploading the pdf file in django form.

But i need to perform the validation on the pdf file by reading the contents of file and then checking and if its not valid then give some error message.

But i am not able to find how can i get the pdf file location so that i can read the file and do some test.

I don't want to first save in temp location and then do validation and then delete the file

all i need is the path of the file so that i can run some system command on the file

1
  • You will have to save it first in order to get a path. Commented Nov 28, 2012 at 3:29

1 Answer 1

1

The example below is based on the Basic file uploads example in the django docs. You can have a function in this example called check_pdf that does your validation on the pdf file and returns true/false based on whether it is valid. Depending on that you redirect the user to a success or invalid pdf page. This is the simplest way of doing it. You could do more and present specific error messages using the forms apis.

from django.http import HttpResponseRedirect from django.shortcuts import render_to_response def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): if check_pdf(request.FILES['file'].name): return HttpResponseRedirect('/success/url/') else: return HttpResponseRedirect('/invalid_pdf/url/') else: form = UploadFileForm() return render_to_response('upload.html', {'form': form}) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.