赞
踩
直接贴代码 自己看 不赘述
qt端:
/// 发送图片 QString picpath = "E:\\PROject\\Qt\\new1008\\ProCocaCola\\savePic\\1.png"; QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png")); imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"pic\"; filename=\""+ picpath + "\"")); // file为后端定义的key,filename即为文件名 QFile *file = new QFile(picpath);//pathbuf1 为文件路径 file->open(QIODevice::ReadOnly); imagePart.setBodyDevice(file); file->setParent(multiPart); multiPart->append(imagePart); //数据 表单 QHttpPart data_part; data_part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain")); //在name属性中设置域名,在这里是userId data_part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"event\"")); //在body体中设置值,在这里是999 data_part.setBody("中文"); multiPart->append(data_part); QUrl url("https://localhost:44356/api/SQLite/Post_UpLoadImageFiles"); QNetworkRequest NetRequest; NetRequest.setUrl(url); QNetworkAccessManager NetAccessManager; QNetworkReply* NetReply = NULL; //https 添加以下 QSslConfiguration config; QSslConfiguration conf = NetRequest.sslConfiguration(); conf.setPeerVerifyMode(QSslSocket::VerifyNone); conf.setProtocol(QSsl::TlsV1SslV3); NetRequest.setSslConfiguration(conf); NetReply = NetAccessManager.post(NetRequest, multiPart); QEventLoop loop; connect(NetReply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); //exec()之前语句会一直处于运行中,直到QEventLoop退出 实现控制程序运行进程 让replay接收完成再做处理 //qDebug()<<"out3:"<<NetReply->header(QNetworkRequest::KnownHeaders::ContentTypeHeader); //记录httpCode int httpsCode = NetReply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug()<<"httpCode"<<httpsCode; QNetworkReply::NetworkError e = NetReply->error(); QString replyStr = NetReply->readAll(); qDebug()<<"replyStr"<<replyStr; if (e) { QString errorMessage = NetReply->errorString(); qDebug()<<"errorMessage:"<<errorMessage; }
api 端:
public string Post_UpLoadImageFiles() //多个参数 { var files = HttpContext.Request.Form.Files; if (files.Count != 0)//如果存在文件 { var text = HttpContext.Request.Form.Files[0].OpenReadStream();//获取到文件流 string strPath = "E:\\PROject\\Qt\\new1008\\ProCocaCola\\savePic\\receive.png";//这里要注意的是先要建立File文件夹,不然会报错,也可以自己写一个检测文件夹,这里就先不赘述 StreamHelp.StreamToFile(text, strPath);//需要用到下一步的帮助类将其保存为文件 var event_ = HttpContext.Request.Form["event"]; return event_.ToString(); } else return "no"; }
更新:
请求url加参数 适用于接口带参数的api
strUrl = "https://localhost:44356/api/SQLite/Post_uploadImageFiles_2?AlarmID=zhao";
//表单的方式 void MainWindow::on_Btn_postPIc_clicked() //url带参数 { QString strUrl; QString strerrorMessage; QString strReply; /// 发送图片 QString picpath = "E:\\PROject\\Qt\\new1008\\ProCocaCola\\savePic\\2.png"; QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png")); QFileInfo fileinfo = QFileInfo(picpath); imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"pic\"; filename=\""+ fileinfo.fileName() + "\"")); // file为后端定义的key,filename即为文件名 QFile *file = new QFile(picpath);//pathbuf1 为文件路径 file->open(QIODevice::ReadOnly); imagePart.setBodyDevice(file); file->setParent(multiPart); multiPart->append(imagePart); //strUrl = "http://154.8.139.34:5000/api/Alarm/Image?alarmID=1"; strUrl = "https://localhost:44356/api/SQLite/Post_picture?alarmID=1"; NetWorkHelper netHelper; netHelper.POSTToApibyForm(strUrl,multiPart,strerrorMessage,strReply); qDebug()<<"strReply"<<strReply; }
/// <summary> /// 上传图片表单 /// </summary> [Route("Post_picture")] [HttpPost] //增 public int Post_picture(int alarmID,IFormCollection files) //多个参数 { int result = -1; FormFileCollection fileCollection = (FormFileCollection)files.Files; if (fileCollection.Count > 0) { IFormFile file = fileCollection[0]; StreamReader reader = new StreamReader(file.OpenReadStream()); string content = reader.ReadToEnd(); string name = file.FileName; string filename = name; if (System.IO.File.Exists(filename)) { System.IO.File.Delete(filename); } using (FileStream fs = System.IO.File.Create(filename)) { file.CopyTo(fs); fs.Flush(); } result = 0; } return result; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。