我使用lcurl
将不同的文件(图像,文档,exes等)上传到服务器。文件上传应使用HTTP multipart/formdata
完成,curl.easy()
:setopt_url(serverUrl)
:setopt_httppost(curl.form()
:add_file('file', filePath, 'image/png', fName,
{'Content-length: ' .. fSize}
))
:setopt_postfields('apikey=' .. apikey)
:perform()
:close()
必须包含文件名及其内容。使用lcurl documentation我编写了以下代码(发送图像):
POST request contains no file field
服务器正在响应#include <opencv2\opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
vector<vector<Point>> polygons;
bool bDraw;
vector<Point> poly;
Mat3b img;
Mat3b layer;
Mat3b working;
void CallBackFunc(int event, int x, int y, int flags, void* userdata)
{
if (event == EVENT_LBUTTONDOWN)
{
//cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
// Refresh
working = layer.clone();
if (!bDraw)
{
// Init your polygon
poly.clear();
bDraw = true;
}
// Add Current Point
poly.push_back(Point(x, y));
// Draw Poly
for (size_t i = 1; i < poly.size(); ++i) {
line(working, poly[i - 1], poly[i], Scalar(0, 255, 0));
}
// Draw Points
for (size_t i = 0; i < poly.size(); ++i) {
circle(working, poly[i], 3, Scalar(0, 0, 255));
}
// Update
imshow("My Window", working);
}
else if (event == EVENT_MOUSEMOVE)
{
//cout << "Mouse move over the window - position (" << x << ", " << y << ")" << endl;
// If drawing, update rect width and height
if (!bDraw) return;
// Refresh
working = layer.clone();
// Draw Poly
for (size_t i = 1; i < poly.size(); ++i) {
line(working, poly[i - 1], poly[i], Scalar(0, 255, 0));
}
// Draw Points
for (size_t i = 0; i < poly.size(); ++i) {
circle(working, poly[i], 3, Scalar(0, 0, 255));
}
// Draw Current line
line(working, poly.back(), Point(x, y), Scalar(0, 255, 0));
// Update
imshow("My Window", working);
}
else if (event == EVENT_LBUTTONDBLCLK)
{
//cout << "Left button double clicked" << endl;
// Refresh
working = layer.clone();
// Add Current Point
poly.push_back(Point(x, y));
// Save poly, draw it on layer
polygons.push_back(poly);
// Draw Poly
for (size_t i = 1; i < poly.size(); ++i)
{
line(working, poly[i - 1], poly[i], Scalar(0, 255, 255));
}
// Draw closed poly
line(working, poly.back(), poly.front(), Scalar(0, 255, 255));
// Draw Points
for (size_t i = 0; i < poly.size(); ++i) {
circle(working, poly[i], 3, Scalar(0, 0, 255));
}
layer = working.clone();
bDraw = false;
// Update
imshow("My Window", working);
}
}
int main(int argc, char** argv)
{
bool bDraw = false;
// Read image from file
img = imread("path_to_image");
// initialize your temp images
layer = img.clone();
working = img.clone();
//if fail to read the image
if (img.empty())
{
cout << "Error loading the image" << endl;
return -1;
}
//Create a window
namedWindow("My Window", 1);
//set the callback function for any mouse event
setMouseCallback("My Window", CallBackFunc, NULL);
//show the image
imshow("My Window", working);
// Wait until user press 'q'
while ((waitKey(1) & 0xFF) != 'q');
// Create cropped images and show / save
for (size_t i = 0; i < polygons.size(); ++i)
{
Mat3b out(img.rows, img.cols, Vec3b(0, 0, 0));
Mat1b mask(img.rows, img.cols, uchar(0));
drawContours(mask, polygons, i, Scalar(255), CV_FILLED);
img.copyTo(out, mask);
Rect box = boundingRect(polygons[i]);
out = out(box).clone();
imshow("Crop #" + to_string(i), out);
waitKey(1);
//imwrite("Crop #" + to_string(i), out);
}
waitKey();
return 0;
}
。
示例代码中哪些不正确?缺少什么?
谢谢!
答案 0 :(得分:0)
解决了它。使用:setopt_postfields
添加apikey
重写file
字段。
这是一张运作良好的草图:
curl.easy()
:setopt_url(url)
:setopt_httppost(curl.form()
:add_content('apikey', apikey, 'text/plain')
:add_file('file', filePath, 'application/octet-stream',
fName, {'Content-length: ' .. fSize}
))
:perform()
:close()