文件上传

第一步:html写UI

    

加入提示信息

    

第二步:qmviews.py文件编写视图函数

@login_Check
def upload_data(request):
    if request.method == "POST":  # 请求方法为POST时,进行处理
        myFile = request.FILES.get("myfile", None)  # 获取上传的文件,如果没有文件,则默认为None
        if not myFile:
            upload_msg = 1
        else:
            try:
                cur_path = os.path.abspath('...')
                CUR_PATH = cur_path + '\\upload\\qm\\'

                destination = open(os.path.join(CUR_PATH, myFile.name), 'wb+')  # 打开特定的文件进行二进制的写操作
                for chunk in myFile.chunks():  # 分块写入文件
                    destination.write(chunk)
                destination.close()
                upload_msg = 2
                writeLog_info('[文件%s],上传成功!' % (myFile.name))
            except Exception as e:
                upload_msg = 3
                writeLog_error('[文件%s],上传失败:%s' % (myFile.name,e))
    return render(request, "../templates/qm/xradar.html", {"upload_msg": upload_msg})

第三步:urls.文件添加路由

    url('^xradar/upload_data/$', qmviews.upload_data),
image.png

image.png

image.png

image.png

image.png

你可能感兴趣的:(文件上传)