0

I have developed an App that takes input file from the upload method without using model and run some code behind the server

like this:

/MyDjango_project_directory/media/input.csv

And generates some result files in a location like this.

/Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.csv
/Virtualenv_directory/MyDjango_project_directory/OutPut_files_directory/result.png

Presently I am just moving the output files into "media" folder through "sys" command in "views.py" just before rendering the result page and through download link one can download the files successfully. This is a temporary solution which is working for me but not the best solution for which I am looking for. Does any body have some batter idea so that I can add my "output_directory" dedicate for the download purposes.

updated

My views:

from django.shortcuts import render
from django.core.files.storage import FileSystemStorage
from django.shortcuts import render, redirect
from django.conf import settings
from django.core.files.storage import FileSystemStorage
import os
import glob
from django.core.files.storage import FileSystemStorage

def Home_page(request):

    return render(request, 'protocol/home.html', {})


#def Main_protocol(request):
   # return render(request, 'protocol/main_protocol.html', {}

def simple_upload(request):
    result_files_list = []
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)

        os.system("python /home/user/Desktop/pep_learn_project/new_pep_src/protocol/PEP_learn_1.0_selected/Sample_protocol.py > pro.log")
        os.system("rm /home/user/Desktop/pep_learn_project/new_pep_src/media/*.csv")


        base_link = "/home/user/Desktop/pep_learn_project/new_pep_src/"
        names = []
        files_to_download = glob.glob("/path_to_files/*.*")

        for i, f in enumerate(files_to_download):
            if f.split(".")[1] in ["csv", "jpg"]:
                names.append(files_to_download[i].split("/")[6])

        return render(request, 'protocol/successful.html', {
            'names': names, 'base_link':base_link
        })

    return render(request, 'protocol/main_protocol.html')

urls:

from django.conf.urls import url
from django.contrib import admin
from . import views

urlpatterns = [
    url(r'^$', views.Home_page, name='Home_page'),
    url(r'^protocol/$', views.simple_upload, name='simple_upload'),
]

template:

{% block content %}

<style type="text/css">



    table {

     margin-bottom: 20px;

     border-collapse: collapse;
     border-spacing: 0;
     width: 30%;
     border: 1px solid #ddd;
     bgcolor: #00FF00;
}

th, td {
    border: none;
    text-align: left;
    padding: 8px;
}

tr:nth-child(even){background-color: #f2f2f2}

</style>



<div style="overflow-x:auto;">
  <table align="center">
    <tr>
      <th align="center">Result files</th>
    </tr>
    {% for a in names %}
    <tr>
     {% if a %}
      <td><a href="/media/{{a}}"> {{a}} </a> <br></td>
     {% endif %}
    </tr>
    {% endfor %}
  </table>
</div>


{% endblock %}
jax
  • 3,927
  • 7
  • 41
  • 70

1 Answers1

1

After struggling a lot finally I have write down what I needed:

Views would be:

def Download_files(request, file_name):

        file_path = os.path.join('/out_put_files', file_name)
        file_wrapper = FileWrapper(file(file_path,'rb'))
        file_mimetype = mimetypes.guess_type(file_path)
        response = HttpResponse(file_wrapper, content_type=file_mimetype )
        response['X-Sendfile'] = file_path
        response['Content-Length'] = os.stat(file_path).st_size
        response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name) 

        return response

and the URLs:

 url(r'^protocol/(?P<file_name>.+)$', views.Download_files, name='Download_files'),

I am answering my own question this means I just learned this little while ago, and posted here so that if some one can be benefited from this. If any expert came across to this answer kindly review it, wither its a ugly solution or an appropriate solution and will it work during deployment also ?. thanks.

This Question has helped me a lot to understand and implement the concept: Downloading the files(which are uploaded) from media folder in django 1.4.3

jax
  • 3,927
  • 7
  • 41
  • 70