Created
January 19, 2018 12:31
-
-
Save madwind76/40ae3ecf4cb882b0a83b34c787cdbc2c to your computer and use it in GitHub Desktop.
qt post image to flask server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---templates | |
# uploads | |
# app.py | |
import os | |
# We'll render HTML templates and access data sent by POST using the | |
# request object from flask. Redirect and url_for will be used to | |
# redirect the user once the upload is done and send_from_directory will | |
# help us to send/show on the browser the file that the user just | |
# uploaded | |
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, jsonify | |
from werkzeug import secure_filename | |
# Initialize the Flask application | |
app = Flask(__name__) | |
# This is the path to the upload directory | |
app.config['UPLOAD_FOLDER'] = 'uploads/' | |
# These are the extension that we are accepting to be uploaded | |
app.config['ALLOWED_EXTENSIONS'] = set(['txt', 'pdf', 'png', 'jpg', | |
'jpeg', 'gif']) | |
# For a given file, return whether it's an allowed type or not | |
def allowed_file(filename): | |
return '.' in filename and filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS'] | |
# This route will show a form to perform an AJAX request jQuery is | |
# loaded to execute the request and update the value of the operation | |
@app.route('/') | |
def index(): | |
return render_template('index.html') | |
# Route that will process the file upload | |
@app.route('/upload', methods=['POST']) | |
def upload(): | |
# Get the name of the uploaded file | |
file = request.files['file'] | |
# Check if the file is one of the allowed types/extensions | |
if file and allowed_file(file.filename): | |
# Make the filename safe, remove unsupported chars | |
filename = secure_filename(file.filename) | |
# Move the file form the temporal folder to the upload folder we | |
# setup | |
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | |
# Redirect the user to the uploaded_file route, which will | |
# basicaly show on the browser the uploaded file | |
#return redirect(url_for('uploaded_file',filename=filename)) | |
return jsonify({'status':'OK'}) | |
# This route is expecting a parameter containing the name of a file. | |
# Then it will locate that file on the upload directory and show it on | |
# the browser, so if the user uploads an image, that image is going to | |
# be show after the upload | |
@app.route('/uploads/<filename>') | |
def uploaded_file(filename): | |
return send_from_directory(app.config['UPLOAD_FOLDER'], | |
filename) | |
if __name__ == '__main__': | |
app.run( | |
host="0.0.0.0", | |
port=int("8080"), | |
debug=True | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> <html lang="en"> | |
<head> | |
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" | |
rel="stylesheet"> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="header"> | |
<h3 class="text-muted">How To Upload a File</h3> | |
</div> | |
<hr/> | |
<div> | |
<form action="upload" method="post" enctype="multipart/form-data"> | |
<input type="file" name="file"><br /><br /> | |
<input type="submit" value="Upload"> | |
</form> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "widget.h" | |
#include "ui_widget.h" | |
#include <QDebug> | |
#include <opencv2/opencv.hpp> | |
#include <QNetworkReply> | |
#include <QFile> | |
using namespace std; | |
Widget::Widget(QWidget *parent) : | |
QWidget(parent), | |
ui(new Ui::Widget) | |
{ | |
ui->setupUi(this); | |
} | |
Widget::~Widget() | |
{ | |
delete ui; | |
} | |
void Widget::on_pushButton_clicked() | |
{ | |
//path image | |
QString path("d:\\people3.jpeg"); | |
//php script that receives the image | |
QNetworkRequest requete(QUrl("http://*.*.*.*:8080/upload")); | |
QByteArray boundary = "------WebKitFormBoundarytoHka8LUGjq34sBN"; | |
QFile file(path); | |
if (!file.open(QIODevice::ReadOnly)) | |
{ | |
qDebug()<<"error read image"; | |
return; | |
} | |
QByteArray fileContent(file.readAll()); | |
QByteArray data = boundary + "\r\n"; | |
data += "Content-Disposition: form-data; name=\"file\"; filename=\"people3.jpeg\"\r\n"; | |
data += "Content-Type: image/jpeg\r\n\r\n" + fileContent + "\r\n"; | |
data += boundary + "--\r\n"; | |
requete.setRawHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundarytoHka8LUGjq34sBN"); | |
requete.setRawHeader("Content-Length", QString::number(data.size()).toLatin1 ()); | |
file.close(); | |
QNetworkAccessManager *am = new QNetworkAccessManager(this); | |
QNetworkReply *reply = am->post(requete,data); | |
QObject::connect(am, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); | |
} | |
void Widget::replyFinished(QNetworkReply *reply) | |
{ | |
reply->open(QIODevice::ReadOnly); | |
// if the response is correct | |
if(reply->error() == QNetworkReply::NoError) | |
{ | |
QByteArray str=(reply->readAll()); | |
QString response = QString::fromUtf8(str.data(), str.size()); | |
qDebug()<<" re "<<response; | |
} | |
//error sever | |
else | |
qDebug()<<"error response server"; | |
} | |
/* | |
result | |
re "{ | |
"status": "OK" | |
}" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment