64,282
社区成员
发帖
与我相关
我的任务
分享
void UploadFileToUrl(const char* url,const char* filename)
{
char* buf;
FILE* file = fopen(filename,"r");
fseek(file,0,SEEK_END);
long len = ftell(file);
fseek(file,0,SEEK_SET);
buf = new char[len];
fread(buf,len,1,file);
fclose(file);
CURL* curlHandle = curl_easy_init();
struct curl_httppost *post=NULL;
struct curl_httppost *last=NULL;
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "pic",
CURLFORM_BUFFER, filename,
CURLFORM_BUFFERPTR, buf,
CURLFORM_BUFFERLENGTH,len,
CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "Submit",
CURLFORM_COPYCONTENTS,"",
CURLFORM_END);
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "action",
CURLFORM_COPYCONTENTS,"uppic",
CURLFORM_END);
/* Set the form info */
curl_easy_setopt(curlHandle,CURLOPT_URL,url);
curl_easy_setopt(curlHandle, CURLOPT_POST, 1);
curl_easy_setopt(curlHandle, CURLOPT_HTTPPOST, post);
curl_easy_perform(curlHandle); /* post away! */
/* free the post data again */
curl_formfree(post);
curl_easy_cleanup(curlHandle);
delete buf;
buf = NULL;
}