长江三角洲城市群数据平台(4)-文件上传、下载

在总结这一节之前,对前面所做的工作的界面进行展示。

Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png

在系统中需要上传下载资料,因此设计了文件上传和下载模块。

1.首先创建disk应用模块。

Paste_Image.png

2.然后对auth中的User模型进行扩展,新建UserProfile模型,要将文件上传至upload,因此对urls要进行设置。

Paste_Image.png
Paste_Image.png
Paste_Image.png
Paste_Image.png

3.编写简单的view,以返回profile.html。

Paste_Image.png

4.新建profile,目录结构如下。

Paste_Image.png

5.profile.html的具体内容如下。

Paste_Image.png

6.改写views,以实现对文件上传的具体逻辑。

Paste_Image.png

7.增加数据上传入口。

Paste_Image.png

8.用bootstrap的文件上传插件对上传功能进行拓展。

{% extends "base.html" %}
{% load staticfiles %}
{% block css %}
    <link href="http://getbootstrap.com/examples/signin/signin.css" rel="stylesheet">
    <link href="http://cdn.bootcss.com/bootstrap-fileinput/4.3.5/css/fileinput.min.css" rel="stylesheet">
{% endblock %}
{% block js %}
    <script src="http://cdn.bootcss.com/bootstrap-fileinput/4.3.5/js/fileinput.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap-fileinput/4.3.5/js/locales/zh.min.js"></script>
{% endblock %}


{% block main %}
    <script src={% static 'js/upload.js' %}></script>

    <link href={% static 'bootstrap/css/fileinput.min.css' %}  media="all" rel="stylesheet" type="text/css" />
    <script {% static 'bootstrap/js/fileinput.min.js' %}></script>
    <script {% static 'bootstrap/js/fileinput_locale_zh.js' %}></script>
     <div class="form-pos">
   <form class="form-signin" role="form" method="POST" enctype="multipart/form-data" >
       {% csrf_token %}
   <fieldset>
       <h2 class="form-signin-heading" >请上传</h2>
             <p>
                <label for="username">用户名</label>
                <input class="form-control"  type="text"  id="username"  name="username"/>
             </p>
              <p>
                <label for="headImg">文件</label>
                <input type="file"  id="headImg" class="file" data-overwrite-initial="false" multiple name="headImg" />
             </p>
             </fieldset>
       </form>
      </div>
      <p class="pull-right" style="font-size: larger">{{ success }}</p>

js配置代码如下。

 $("#headImg").fileinput({
            language: 'zh', //设置语言
            //uploadUrl: 'media/profile', // you must set a valid URL here else you will get an error
            //allowedFileExtensions : ['jpg','png'],
            allowedFileExtensions: ['csv', 'txt'],
            maxFileSize: 60000000,
            maxFilesNum: 10,
            //allowedFileTypes: ['image', 'video', 'flash'],
            slugCallback: function (filename) {
                return filename.replace('(', '_').replace(']', '_');
            }
        });

9.下载通过js即可实现。

Paste_Image.png
function goto(url){
             window.location=url;
         }
         var url="#"
          $('#down_select').on('change',function(){
              var checkValue=$("#down_select").val();
              console.log(checkValue);
              checkValue.replace("//","\\");
              url='\\'+'media'+'\\'+checkValue;
              console.log(url);
          });

         $("#down_button").click(function(){goto(url)});

10界面如下,未进行美化。

Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,393评论 25 708
  • //我所经历的大数据平台发展史(三):互联网时代 • 上篇http://www.infoq.com/cn/arti...
    葡萄喃喃呓语阅读 51,321评论 10 200
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,957评论 19 139
  • 作天作地作阿爸,如下,並伐老最終答應給她看日記,但又拿給她看的日期來賴皮了,嘿嘿,不管了,一旦給她看了,我心裡還是...
    帶風走路deFENG阅读 189评论 0 1
  • 漆黑的夜,抬头看不见一颗星,望不到那颗月。 百无聊赖,一少年沿着村中小路,一条,一条的走,曲曲折折...
    星河璀璨L阅读 402评论 0 1