我需要将图像上传到服务器我使用文件选择器来获取图像,我附上了我的示例代码,但它不起作用
我的文件选择器代码
` Button {
text: "Change Profile Image"
onClicked {
filePicker.open();
}
attachedObjects: [
FilePicker {
id: filePicker
property string selectedFile
type: FileType.Picture
onFileSelected: {
selectedFile = selectedFiles[0]
image.imageSource = selectedFile
_settings.showToast( selectedFile);
_settings.isUpdate(selectedFile);
}
}
]`
我在 C++ 中的图像更新代码
`void Settings::isUpdate(QString selectedFile){
QString path("asset:///Icon/mess.jpg");
//php script that receives the image
QNetworkRequest requete(QUrl("http://192.168.1.176:2222/api?type=uploadimages&name=IMG_00000001.jpg&pin=123456")); //our server with php-script
QByteArray boundary = "-------------------------87142694621188";
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
{
qDebug()<<"erreur read image";
return;
}
QByteArray fileContent(file.readAll());
QByteArray data = "--" + boundary + "\r\n";
data += "Content-Disposition: form-data; name=\"data\"; filename=\"IMG_00000001.jpg\";\r\n";
data += "Content-Type: image/jpg\r\n\r\n" + fileContent + "\r\n";
data += "--" + boundary + "--\r\n";
requete.setRawHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
requete.setRawHeader("Content-Length", QString::number(data.size()).toAscii());
file.close();
qDebug() <<"data"<< data.size();
QNetworkAccessManager *am = new QNetworkAccessManager(this);
QNetworkReply *reply = am->post(requete,"&data="+data);
QObject::connect(am, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}
void Settings::replyFinished(QNetworkReply *reply)
{
reply->open(QIODevice::ReadOnly);
if(reply->error() == QNetworkReply::NoError)
{
QByteArray str=(reply->readAll());
QString response = QString::fromUtf8(str.data(), str.size());
qDebug()<<" re "<<response;
}
else
qDebug()<<"error response server";
}`
我可以得到响应,但无法将图像上传到服务器
谢谢