T实现上传多文件和Json数据的进度条

这篇具有很好参考价值的文章主要介绍了T实现上传多文件和Json数据的进度条。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

这里功能主要是实现上传多个文件和Json数据同时上传,并显示上传的进度

采用请求方式:POST
类名:ObjectStorage文章来源地址https://www.toymoban.com/news/detail-468402.html

头文件:objectstorage.h

#ifndef OBJECTSTORAGE_H
#define OBJECTSTORAGE_H

#include <QWidget>
#include "http_request.h"
#include "qlistwidget.h"
#include "menu_control.h"

namespace Ui {
   
class ObjectStorage;
}
class ObjectStorage : public QWidget
{
   
    Q_OBJECT
    QThread HttpObjSThread;
public:
    explicit ObjectStorage(QWidget *parent = nullptr);
    ~ObjectStorage();
    //设置token和api url 并获取域名商列表
    void start_objectStorage();
    //设置disable选项框
    void set_checkbox_disabled(QString corporation,QString account_name,QString bucket_name);

private:
    Ui::ObjectStorage *ui;
    //HTTP客户端
    Http_request *http_client;
    const QString api_url = Menu_control::init_maps["api_url"];
    const QString username=Menu_control::u_username;
    const QString token = Menu_control::u_token;
    const QList<qint32>permission_list = Menu_control::permission_list;

    //请求
    enum class BUTTON_CLICK{
   no_action,get_corporation_list,get_account_list,get_bucket_list,get_bucket_file_list,
                            get_current_bucket_path_list,get_download_file, post_upload_file,put_mkdir_or_del};
    struct HTTP_QUEUE
    {
   
        BUTTON_CLICK action;
        qint32 queue_id;
    };
    QList<HTTP_QUEUE>http_queue;
    //clean
    void clean_data();
    //搜索路径下的内容
    void current_bucket_path_list(QString path);

    //http请求队列
    qint32 get_queue_id();

signals:
    void http_get(QString url,qint32 queue_id);
    void http_download(QString url, QString save_file_path, qint32 queue_id);
    //提交数据信号
    void http_put(QString url,QJsonObject data);
    //上传文件
    void http_upload(QString url,QJsonObject data);

private slots:
    //获取运营商下的所有账号
    void on_corporation_comboBox_activated(int index);
    //获取账号下的所有bucket
    void on_account_comboBox_activated(int index);

    //获取数据结束
    void http_get_finished(bool status,QJsonObject data);
    //提交数据请求结束
    void http_put_finished(bool status,QJsonObject data);
    //下载文件结束
    void http_download_or_upload_finished(bool status, QJsonObject data);

    //查询单个bucket下所有文件
    void on_current_bucket_comboBox_activated(int index);
    //复制源站链接
    void on_copy_source_link_pushButton_clicked();
    //复制加速站链接
    void on_copy_acclerate_link_pushButton_clicked();
    //返回上一层目录
    void on_previous_path_pushButton_clicked();

    //点击ListWidgetItems信号槽
    void click_items(QListWidgetItem *item);
    //下载文件
    void on_download_file_pushButton_clicked();
    //上传文件
    void on_upload_file_pushButton_clicked();
    //刷新当前路径文件
    void on_current_path_pushButton_clicked();
    //新建目录
    void on_mkdir_pushButton_clicked();
    //删除bucket对象
    void on_delete_bucket_object_pushButton_clicked();
    //下载上传进度条
    void update_progressbar(qint64 bytesReceived_or_sent, qint64 bytesTotal);
};

#endif // OBJECTSTORAGE_H


源文件:objectstorage.cpp

#include "objectstorage.h"
#include "ui_objectstorage.h"
#include "ui_helper.h"
#include "core_common.h"
#include "base64.h"


ObjectStorage::ObjectStorage(QWidget *parent)
    : QWidget{
   parent},
    ui(new Ui::ObjectStorage)
{
   
    ui->setupUi(this);
    ui->save_file_label->setText(QDir::homePath()+QString("/Downloads/"));
    ui->handle_progressBar->setValue(0);
    http_client = new Http_request();
    http_client->set_exist_token(token);

    //建立信号槽
    //GET信号
    connect(this,&ObjectStorage::http_get,http_client,&Http_request::http_get);
    connect(http_client,&Http_request::http_get_finished,this,&ObjectStorage::http_get_finished);  

    //PUT信号
    connect(this,&ObjectStorage::http_put,http_client,&Http_request::http_put);
    connect(http_client,&Http_request::http_put_finished,this,&ObjectStorage::http_put_finished);

    //下载
    connect(this,&ObjectStorage::http_download,http_client,&Http_request::http_download);
    connect(http_client,&Http_request::http_download_finished,this,&ObjectStorage::http_download_or_upload_finished);

    //上传
    connect(this,&ObjectStorage::http_upload,http_client,&Http_request::http_upload);
    connect(http_client,&Http_request::http_upload_finished,this,&ObjectStorage::http_download_or_upload_finished);

    //进度条
    connect(http_client,&Http_request::updateProgressBar,this,&ObjectStorage::update_progressbar);

    //线程开始
    http_client->moveToThread(&HttpObjSThread);
    //线程关闭
    connect(&HttpObjSThread,&QThread::finished,http_client,&QObject::deleteLater);
    //线程开始
    HttpObjSThread.start();

    //QListWidget信号槽
    connect(ui->object_listWidget,&QListWidget::itemClicked,this,&ObjectStorage::click_items);
}

ObjectStorage::~ObjectStorage(){
   
    http_client = nullptr;
    delete http_client;
    HttpObjSThread.quit();
    HttpObjSThread.wait();
    delete ui;
}

//获取随机请求id
qint32 ObjectStorage::get_queue_id(){
   
    return QRandomGenerator::global()->bounded(1, 9999999);
}

//设置token和api url 并获取域名商列表
void ObjectStorage::start_objectStorage(){
   
    QByteArray byte_data = QString(
                "{\"t_time\":%1,\"function_id\":%2,\"random_str\":\"%3\"}"
                ).arg(Core_common::get_timestamp(),QString::number(3),Core_common::get_random_str(15)).toLocal8Bit();
    QString data = MyBase64::get_base64_encode_2(byte_data.toHex());

    QtConcurrent::run([=]{
   
        HTTP_QUEUE action;
        qint32 queue_id = get_queue_id();
        action.action = BUTTON_CLICK::get_corporation_list;
        action.queue_id = queue_id;
        http_queue<<action;
        emit http_get(api_url+"api/cloud/oss?account_name="+data+"&common_area=3382210&lang=zh_cn",queue_id);
    });
}
//设置运营商,账户,bucket
void ObjectStorage::set_checkbox_disabled(QString corporation,QString account_name,QString bucket_name){
   
    ui->corporation_comboBox->addItem(corporation);
    ui->corporation_comboBox->setCurrentText(corporation);
    ui->corporation_comboBox->setDisabled(true);
    ui->account_comboBox->addItem("");
    ui->account_comboBox->addItem(account_name);
    ui->account_comboBox->setCurrentText(account_name);
    ui->account_comboBox->setDisabled(true);
    ui->current_bucket_comboBox->addItem("");
    ui->current_bucket_comboBox->addItem(bucket_name);
    ui->current_bucket_comboBox->setDisabled(true);
    ui->current_bucket_comboBox->setCurrentText(bucket_name);
    ui->current_path_lineEdit->setText("/");
    on_current_path_pushButton_clicked();
}

// 运营商选项栏信号槽 ->查询运营商下所有账户
void ObjectStorage::on_corporation_comboBox_activated(int index)
{
   
    ui->account_comboBox->clear();
    ui->current_bucket_comboBox->clear();
    clean_data();
    if (index == 0){
   
        return ;
    }

    QByteArray byte_data = QString(
                "{\"t_time\":%1,\"function_id\":%2,\"platform\":\"%3\",\"random_str\":\"%4\"}"
                ).arg(Core_common::get_timestamp(),QString::number(4),ui->corporation_comboBox->itemText(index),Core_common::get_random_str(15)).toLocal8Bit();
    QString data = MyBase64::get_base64_encode_2(byte_data.toHex());

    QtConcurrent::run([=]{
   
        HTTP_QUEUE action;
        qint32 queue_id = get_queue_id();
        action.action = BUTTON_CLICK::get_account_list;
        action.queue_id = queue_id;
        http_queue<<action;
        emit http_get(api_url+"api/cloud/oss?account_name="+data+"&common_area="+queue_id+"&lang=zh_cn",queue_id);
    });
}

//获取账号下的所有bucket
void ObjectStorage::on_account_comboBox_activated(int index)
{
   
    ui->current_bucket_comboBox->clear();
    clean_data();
    if((index == 0) | (ui->corporation_comboBox->currentIndex() == 0) ){
   return;}

    QByteArray byte_data = QString(
                "{\"t_time\":%1,\"function_id\":%2,\"platform\":\"%3\",\"account_name\":\"%4\",\"random_str\":\"%5\"}"
                ).arg(Core_common::get_timestamp(),QString::number(5),ui->corporation_comboBox->currentText(),ui->account_comboBox->itemText(index),
                      Core_common::get_random_str(15)).toLocal8Bit();
    QString data = MyBase64::get_base64_encode_2(byte_data.toHex());

    QtConcurrent::run([=]{
   
        HTTP_QUEUE action;
        qint32 queue_id = get_queue_id();
        action.action = BUTTON_CLICK::get_bucket_list;
        action.queue_id = queue_id;
        http_queue<<action;
        emit http_get(api_url+"api/cloud/oss?account_name="+data+"&common_area=3382210&lang=zh_cn",queue_id);
    });
}


//获取bucket下目录查询
void ObjectStorage::on_current_bucket_comboBox_activated(int index)
{
   
    clean_data();
    if(index == 0){
   return;}
    QByteArray byte_data = QString(
                               "{\"t_time\":%1,\"function_id\":%2,\"platform\":\"%3\",\"account_name\":\"%4\",\"bucket_name\":\"%5\",\"random_str\":\"%6\"}"
                               ).arg(Core_common::get_timestamp(),QString::number(6),ui->corporation_comboBox->currentText(),
                                    ui->account_comboBox->currentText(),ui->current_bucket_comboBox->itemText(index),Core_common::get_random_str(15)).toLocal8Bit();
    QString data = MyBase64::get_base64_encode_2(byte_data.toHex());

    QtConcurrent::run([=]{
   
        HTTP_QUEUE action;
        qint32 queue_id = get_queue_id();
        action.action = BUTTON_CLICK::get_bucket_file_list;
        action.queue_id = queue_id;
        http_queue<<action;
        emit http_get(api_url+"api/cloud/oss?account_name="+data+"&common_area=3382210&lang=zh_cn",queue_id);
    });
}

//清理数据
void ObjectStorage::clean_data(){
   
    ui->object_listWidget->clear();
    ui->current_bucket_location_label->clear();
    ui->current_path_lineEdit->clear();
    ui->copy_source_link_lineEdit->clear();
    ui->copy_accelerate_link_lineEdit->clear();
    ui->download_click_file_label->clear();
    ui->handle_progressBar->setValue(0);
    ui->upload_console_plainTextEdit->clear();
}

//复制源站链接
void ObjectStorage::on_copy_source_link_pushButton_clicked()
{
   
    QClipboard *clip = QApplication::clipboard();
    clip->setText(ui->copy_source_link_lineEdit->text());
}

//复制加速源链接
void ObjectStorage::on_copy_acclerate_link_pushButton_clicked()
{
   
    QClipboard *clip = QApplication::clipboard();
    clip->setText(ui->copy_accelerate_link_lineEdit->text());
}

//返回上一层目录
void ObjectStorage::on_previous_path_pushButton_clicked()
{
   
    ui->download_click_file_label->clear();
    QString current_path = ui->current_path_lineEdit->text();
    if(current_path.isEmpty()){
   
        return;
    }
    if(current_path == "/"){
   
        UI_helper::showMessageBox("Warning","已是最顶层目录,\nThe currently is the topmost directory",2);
        return;
    }
    //查找倒数第二次出现所在的位置,字符串截断,截取到所在位置
    current_bucket_path_list(current_path.mid(0,(current_path.lastIndexOf("/",-2))+1));
}

//点击ListWidget中单个items信号槽
void ObjectStorage::click_items(QListWidgetItem *item){
   
    QString item_name = item->text();
    if(item_name.contains("/")){
   
        QString current_path = ui->current_path_lineEdit->text();
        current_path+=item_name;
        current_bucket_path_list(current_path);
    }else{
   
        ui->download_click_file_label->setText(item_name);
        QString current_source_link = ui->copy_source_link_lineEdit->text();
        current_source_link = current_source_link.mid(0,(current_source_link.lastIndexOf("/",-1)+1));
        ui->copy_source_link_lineEdit->setText(current_source_link+item_name);
        QString current_accelerate_link = ui->copy_accelerate_link_lineEdit->text();
        current_accelerate_link = current_accelerate_link.mid(0,(current_accelerate_link.lastIndexOf("/",-1)+1));
        ui->copy_accelerate_link_lineEdit->setText(current_accelerate_link+item_name);
    }
}

//搜索路径下的内容
void ObjectStorage::current_bucket_path_list(QString path){
   
    ui->object_listWidget->clear();
    ui->upload_console_plainTextEdit->clear();
    QByteArray byte_data = QString(
                               "{\"t_time\":%1,\"function_id\":%2,\"platform\":\"%3\",\"account_name\":\"%4\",\"prefix\":\"%5\","
                               "\"bucket_name\":\"%6\",\"random_str\":\"%7\"}"
                               ).arg(Core_common::get_timestamp(),QString::number(7),ui->corporation_comboBox->

到了这里,关于T实现上传多文件和Json数据的进度条的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包