• 上传单个文件


    使用一个模型来处理上传文件
    
    node2:/django/mysite/news/templates#cat upload.html 
    <div class="row">
          <div class="col-md-8 col-md-offset-2">
              <form class="form-inline" role="form"  method="post" enctype="multipart/form-data" accept-charset="utf-8">
                  <div class="form-group">
                      <input type="file" name="file">
                  </div>
                  <div class="form-group">
                      <input type="submit" value="上传文件">
                  </div>
              </form>
          </div>
      </div>
    
    node2:/django/mysite/mysite#cat urls.py |grep upload
    url(r'^upload/$',newview.upload,name='upload'),
    
    def upload(request):
        if request.method=="POST":
            print '1111111---------------------'
            print request.FILES['file']
            print '1111111---------------------'
            print '2222222---------------------'
            print str(request.FILES['file'])
            print '2222222---------------------'
            handle_upload_file(request.FILES['file'],str(request.FILES['file']))
            return HttpResponse('Successful') #此处简单返回一个成功的消息,在实际应用中可以返回到指定的页面中  
        else:
         return render_to_response('upload.html')
    
    def handle_upload_file(file,filename):
        path='/tmp/'     #上传文件的保存路径,可以自己指定任意的路径  
        if not os.path.exists(path):
            os.makedirs(path)
        with open(path+filename,'wb+')as destination:
            for chunk in file.chunks():
                print '-----------'
                print chunk
                print '-----------'
                destination.write(chunk)
    			

  • 相关阅读:
    贪婪大陆
    色板游戏
    11/29 NOIP 模拟赛
    USACO4.4 重叠的图像 Frame Up
    CSP2020 题解
    NOIP前板子复习
    关于我
    【洛谷】【搜索+字符串】
    【洛谷】【动态规划/01背包】P2925 [USACO08DEC]干草出售Hay For Sale
    【洛谷】【二分查找】P1102 A−B数对
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349279.html
Copyright © 2020-2023  润新知