• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Django Post上传文件和传递参数

武飞扬头像
滚雪球~
帮助1

前端js上传文件

        <div class="ac-game-settings-acwing">
            <br>
            <input type="file" name="file" id="file_select">
            <input type="button" value="上传" class="file_upload">
        </div>
    add_listening_events() {
        console.log('ok');
        let outer = this;
        this.add_listening_events_login();
        this.add_listening_events_register();
    }
    add_listening_events_login() {
        let outer = this;
        console.log('123');
        this.$login_register.click(function () {
            console.log('click register');
            outer.register();
        });
        this.$login_submit.click(function () {
            outer.login_on_remote();
        });
        this.$upload_file.click(function () {
            outer.FileUpload();
        });
    }
    FileUpload() {
        var form_data = new FormData();
        var file_info = $('#file_select')[0].files[0];
        form_data.append('file', file_info);
        if (file_info == undefined) {
            alert('你没有选择任何文件');
            return false;
        }
        $.ajax({
            url: target_url   "/files/file_up/",
            type: 'POST',
            data: form_data,
            processData: false,  // tell jquery not to process the data
            contentType: false, // tell jquery not to set contentType
            success: function (callback) {
                alert('上传成功');
            }
        });
    }
学新通

后端获取ajax

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
@csrf_exempt
def upload_ajax(request):
    print(request.FILES.get('file').name)
    if request.method == 'POST':
        file_obj = request.FILES.get('file')
        import os
        # JS_PATH=/home/fjtx/ai_asian_back/demo/static/js/
        path1 = os.path.abspath(os.path.join(os.getcwd()))
        print(path1)
        #需要存储的地址
        f = open(os.path.join(path1, 'static', 'file', file_obj.name), 'wb')
        print(file_obj,type(file_obj))
        for chunk in file_obj.chunks():
            f.write(chunk)
        f.close()
        print('11111')
        return JsonResponse({
            'result': "success"
        })
学新通

js前端进行上传
学新通

学新通

Post传递参数

参考:https://blog.csdn.net/a582816317/article/details/79100754
前端js代码

    FileUpload() {
        var form_data = new FormData();
        var file_info = $('#file_select1')[0].files[0];
        console.log(file_info)
        form_data.append('file', file_info);
        form_data.append('username', this.root.settings.username);//传递参数
        if (file_info == undefined) {
            alert('你没有选择任何文件');
            return false;
        }
        $.ajax({
            url: target_url   "/files/file_up/",
            type: 'POST',
            data: form_data,
            processData: false,  // tell jquery not to process the data
            contentType: false, // tell jquery not to set contentType
            success: function (callback) {
                alert('上传成功');
            }
        });
    }
学新通

后端python代码

from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
@csrf_exempt
def upload_ajax(request):
    print(request.FILES.get('file').name)#接收文件
    username=request.POST.get('username')#接收参数
    print(username)
    if request.method == 'POST':
        file_obj = request.FILES.get('file')

        import os
        # JS_PATH=/home/fjtx/ai_asian_back/demo/static/js/
        path1 = os.path.abspath(os.path.join(os.getcwd()))
        print(path1)
        #需要存储的地址
        f = open(os.path.join(path1,'demo', 'static', 'file', file_obj.name), 'wb')
        for chunk in file_obj.chunks():
            f.write(chunk)
        f.close()
        return JsonResponse({
            'result': "success"
        })
学新通

Django返回列表

学新通
前端
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgbhake
系列文章
更多 icon
同类精品
更多 icon
继续加载