c++ django上传图片

来源:互联网 发布:模拟网络攻击软件 编辑:程序博客网 时间:2024/06/06 11:57


python部分:

urlpatterns = [    url(r'^$', views.home),    url(r'index', views.Index),    url(r'pic_class', views.pic_class),    url(r'^admin/', admin.site.urls),]


def pic_class(request):    upload_file = request.FILES.get("buffer", None)  # 获取上传的文件,如果没有文件,则默认为None    file_obj = request.FILES.get('myfile')    if file_obj:   # 处理附件上传到方法        request_set = {}        print('file--obj', file_obj)        # user_home_dir = "upload/%s" % (request.user.userprofile.id)        # accessory_dir = settings.accessory_dir        # if not os.path.isdir(accessory_dir):        #     os.mkdir(accessory_dir)        scr = Image.open(file_obj)        scr.show()        img= np.asarray(scr)


c++代码:

void upload_file() {

CURL *curl;

CURLcode res;

 

struct curl_httppost *formpost =NULL;

struct curl_httppost *lastptr =NULL;

struct curl_slist *headerlist =NULL;

static const char buf[] ="Expect:";

 

Mat image = imread("d:\\11.jpeg", CV_LOAD_IMAGE_COLOR);

 

IplImage iplimage = image;

 

vector<uchar> buff;//buffer for coding

 

vector<int> param = vector<int>(2);

 

param[0] = CV_IMWRITE_JPEG_QUALITY;

 

param[1] = 95;//default(95) 0-100

 

 

 

imencode(".jpg", image, buff, param);

std::string str_encode(buff.begin(), buff.end());

 

 

//stringstream serializedStream = serialize(image);

int size = image.total() * image.elemSize();

byte * bytes = new byte[size];  // you will have to delete[] that later

std::memcpy(bytes, image.data, size * sizeof(byte));

curl_global_init(CURL_GLOBAL_ALL);

/* Fill in the file upload field */

curl_formadd(&formpost,

&lastptr,

CURLFORM_COPYNAME, "message",

CURLFORM_COPYCONTENTS, "hello",

CURLFORM_END);

 

/* Add a buffer to upload */

curl_formadd(&formpost, &lastptr,

CURLFORM_COPYNAME, "buffer",

CURLFORM_BUFFER, "data",

CURLFORM_BUFFERPTR, str_encode.c_str(),

CURLFORM_BUFFERLENGTH, str_encode.length(),

CURLFORM_CONTENTTYPE, "image/jpeg",

CURLFORM_END);

 

curl_formadd(&formpost,

&lastptr,

CURLFORM_COPYNAME, "myfile",

CURLFORM_FILE, "11.jpeg",

//CURLFORM_CONTENTTYPE, "application/vnd.ms-excel",

CURLFORM_END);

 

curl = curl_easy_init();

/* initalize custom header list (stating that Expect: 100-continue is not

wanted */

headerlist = curl_slist_append(headerlist, buf);

if (curl) {

/* what URL that receives this POST */

curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8000/pic_class/");

//if ((argc == 2) && (!strcmp(argv[1], "noexpectheader")))

/* only disable 100-continue header if explicitly requested */

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);

curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

 

/* Perform the request, res will get the return code */

res = curl_easy_perform(curl);

/* Check for errors */

if (res != CURLE_OK)

fprintf(stderr, "curl_easy_perform() failed: %s\n",

curl_easy_strerror(res));

 

/* always cleanup */

curl_easy_cleanup(curl);

 

/* then cleanup the formpost chain */

curl_formfree(formpost);

/* free slist */

curl_slist_free_all(headerlist);

}

}


原创粉丝点击