0

I'm working on a project where user uploads a file during creating the order. The file is sent through AJAX to a view and then recognized number of words which returns back.

For this purpose, I want to use textract module. The problem is that I haven't found a way how to add an inmemory uploaded file as an textract.process(filepath) argument since it wants filepath, not a file.

So I suppose I should save this file temporary on the drive and send a path to this file to process function.

@csrf_exempt
def ajax_file_word_count(request):
    from SolutionsForLanguages import word_counter
    file = request.FILES.get('file')
    count = word_counter.count_words(file)
    return JsonResponse({'word_count': count})

How can I do that so the file is immediately deleted after words are counted?

Milano
  • 18,048
  • 37
  • 153
  • 353

1 Answers1

0

Look at pythons tempfile module. You can use it to create a temporary file and using that file objects path property, get the file path then pass it to your intended function. I guess it should work and then you can delete the temp file.

Rajesh Yogeshwar
  • 2,111
  • 2
  • 18
  • 37