excel文件导入,显示文件不存在

后端代码

def import_orders_excel(request):
    # 批量导入数据
    # 接收excel文件并存储到media文件夹
    # 接收上传文件
    rev_file = request.FILES.get('excel')
    # 判断是否有文件
    if not rev_file:
        return JsonResponse({'code': 0, 'msg': '文件不存在!'})
    new_name = get_random_str()
    # 写入的URL
    file_path = os.path.join(settings.MEDIA_ROOT, new_name + os.path.splitext(rev_file.name)[1])
    # 写入本地
    try:
        f = open(file_path, 'wb')
        # 多次写入
        for i in rev_file.chunks():
            f.write(i)
        # 关闭
        f.close()

    except Exception as e:
        return JsonResponse({'code': 0, 'msg': str(e)})
    # 读取存储在media中的数据
    ex_orders = read_excel_dict(file_path)
    # 把读取的数据存储到数据库
    success = 0
    error = 0
    error_order_names = []
    for one_order in ex_orders:
        try:
            obj_order = Orders.objects.create(order_name=one_order['order_name'], product_name=one_order['product_name'],
                                products_size=one_order['products_size'], num=one_order['num'],
                                sold_price=one_order['sold_price'], pakaging=one_order['pakaging'],)
            success += 1
        except:
            error += 1
            error_order_names.append(one_order['order_name'])
    #返回
    obj_orders = Orders.objects.all().values()
    orders = list(obj_orders)
    return JsonResponse({'code': 1, 'success': success, 'error': error, 'errors': error_order_names, 'data': orders})

前端vue代码

uploadExcelPost(file) {
        let that = this
        //定义一个formdata类
        let fileReq = new FormData();
        //把文件传进去
        fileReq.append('excel', file.file);
        //使用axios发起ajax请求
        axios(
          {
            methods: 'post',
            url: that.baseURL + "/apps/ordersmanagement/excel/import/",
            data: fileReq
          }
        ).then(res => {
          //根据code判断是否成功
          if (res.data.code === 1) {
            that.orders = res.data.data;
            // 总共数据条数
            that.total = res.data.data.length;
            //分页
            that.getpageOrders();
            //弹出框显示结果
            this.$alert('本次导入完成,成功:' + res.data.success +
              '条,失败:' + res.data.error + '条。',
              '导入结果展示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$message({
                  type: 'info',
                  message: "本次导入失败数量为:" + res.data.error + ",具体的学号:"+res.data.errors,
                });
              }
            });
            //失败明细
            console.log("本次导入失败的的订单号:");
            console.log(res.data.errors);
          } else {
            //s失败提示
            that.$message.error(res.data.msg)
          }
        }).catch(err => {
          console.log(err);
          that.$message.error('上传excel文件出现异常!');
        })
      },

图片说明

https://www.cnblogs.com/yanchaohui/p/10070361.html

rev_file = request.FILES.get('excel')代码get参数应该是文件名称把,把excel为实际文件名称(注意带上路径)试试

rev_file = request.FILES.get('excel')
修改为

request.FILES['excel']
或者
request.FILES[0]
看看