0

我已经开发了一个应用程序,从上传方法需要输入文件,而无需使用模型和运行一些代码服务器如何使用Django服务Result文件?

这样的背后:

/MyDjango_project_directory/media/input.csv 

而且在这样的位置产生一些结果文件。

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

目前我只是移动输出文件到“媒体”只是渲染页面之前,通过下载链接一个通过“views.py”“SYS”命令文件夹都可以成功下载文件。这是为我工作的临时解决方案,但不是我寻找的最佳解决方案。是否有任何机构有一些连击想法,以便我可以添加我的“output_directory”专门用于下载目的。

更新

我的观点:

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') 

网址:

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'), 
] 

模板:

{% 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 %} 
+0

能否请您在这里分享您的看法码? – viveksyngh

+0

@viveksyngh更新谢谢 – jax

回答

0

挣扎了很多之后最后我都写下来,我需要的东西:

意见是:

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 

和网址:

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

我回答我的问题这意味着我刚刚得知这个小会儿前,并张贴在这里,如果有人可以从中受益。如果有任何专家遇到这个问题,请仔细检查一下,否则它会变成一个丑陋的解决方案或适当的解决方案,并且在部署过程中它还能工作吗?谢谢。

这个问题已经帮了我很多理解和贯彻的理念:Downloading the files(which are uploaded) from media folder in django 1.4.3

相关问题