2

I'm currently building an application that will be used and maintained by another developer shortly in the future. What I want is the ability to upload a zip file, unzip and process the contents, and discard the file without ever actually storing it in the Django file storage system. These are the relevant parts of what I have right now:

views.py:

def upload(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        if str(myfile.name).endswith('.zip'):

            ## THIS STORES THE FILE -- NOT WHAT I WANT
            #fs = FileSystemStorage()
            #filename = fs.save(myfile.name, myfile)

            uploaded_file_url = str(myfile.name)
            return render(request, 'webapp/upload.html', {
                'uploaded_file_url': uploaded_file_url
            })
        file_error = "There was an error processing the file"
        return render(request, 'webapp/upload.html', {
            'file_error': file_error
        })
    return render(request, 'webapp/upload.html')

upload.html

{% extends "./base.html" %}

{% block content %}
<body>
  <form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="myfile">
    <button type="submit">Upload</button>
  </form>

  {% if uploaded_file_url %}
    <p>File uploaded at: <a href="{{ uploaded_file_url }}">{{ uploaded_file_url }}</a></p>
  {% endif %}

  {% if file_error %}
    <p>There was an error in the file submission.</p>
  {% endif %}
</body>
{% endblock %}

I know that checking if a file ends with .zip is not necessarily indicative of whether it's actually a zip file or not but it is sufficient for my purposes right now. myfile is a UploadedFile and I'm trying to find a way to unzip and process the contents but I'm not sure how to go about this. I could just store it in the FileSystem and then process it from there but I'd like to avoid storing it at all if possible. Any suggestions on how to do this would be greatly appreciated.

chemdog95
  • 366
  • 4
  • 16

0 Answers0