2010-04-25 35 views
0
def upload_file(request, step_id): 
    def handle_uploaded_file (file): 
     current_step = Step.objects.get(pk=step_id) 
     current_project = Project.objects.get(pk=current_step.project.pk) 

     path = "%s/upload/file/%s/%s" % (settings.MEDIA_ROOT, current_project.project_no, current_step.name) 
     if not os.path.exists (path): 
      os.makedirs(path) 
     fd = open(path) 
     for chunk in file.chunks(): 
      fd.write(chunk) 
     fd.close() 

    if request.method == 'POST': 
     form = UploadFileForm(request.POST, request.FILES) 
     if form.is_valid(): 
      handle_uploaded_file(request.FILES['file']) 
      return HttpResponseRedirect('/success/url/') 
    else: 
     form = UploadFileForm() 
    return render_to_response('projects/upload_file.html', { 
                 'step_id': step_id, 
                 'form': form, 
                 }) 

回答

2

確保path具有必要的權限。運行python/django進程的用戶需要具有write權限。 chmod0777的路徑 - 這不是一個好的生產模式,但它會很快驗證文件系統權限是否是問題的根源。

+0

謝謝,我怎麼chmod的路徑? – Semanty 2010-04-25 06:13:22

+0

假設你有shell訪問權限,運行'chmod -R 0777 path/to/uploads' – Matt 2010-04-25 06:14:10

+0

對不起,我真的不知道chmod以及如何使用shell來運行chmod。 – Semanty 2010-04-25 06:23:05

相關問題