如何在c ++中上传图像 [英] How to upload image in c++

查看:67
本文介绍了如何在c ++中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好所有





i是c ++的新词

并努力上传图片到一个php web服务器。



但它不能在下面工作是我的代码



亲切的拥有一个看看并帮助我...






hello all


i am new in c++
and working hard to upload an image to a php web Server.

but its not working below is my code

kindly have a look and help me...



  #include <windows.h>
  #include <wininet.h>
  #include <iostream>
  #include <tchar.h>

#pragma comment(lib,"wininet.lib")
  #define ERROR_OPEN_FILE       10
  #define ERROR_MEMORY          11
  #define ERROR_SIZE            12
  #define ERROR_INTERNET_OPEN   13
  #define ERROR_INTERNET_CONN   14
  #define ERROR_INTERNET_REQ    15
  #define ERROR_INTERNET_SEND   16

  using namespace std;

  int main()
  {
     // Local variables
	 static char *filename   = "file";   //Filename to be loaded
	 static char *filepath   = "d:\\a.jpg";   //Filename to be loaded
     static char *type       = "image/jpeg";
     static char boundary[]  = "--BOUNDARY---";            //Header boundary
     static char nameForm[]  = "formname";     //Input form name
     static char iaddr[]     = "localhost";        //IP address
     static char url[]       = "/http/file.php";         //URL
	 
	 char hdrs[512]={'-'};                  //Headers
     char * buffer;                   //Buffer containing file + headers
     char * content;                  //Buffer containing file
     FILE * pFile;                    //File pointer
     long lSize;                      //File size
     size_t result;                   


     // Open file
     pFile = fopen ( filepath , "rb" );
     if (pFile==NULL) 
	 {
		 printf("ERROR_OPEN_FILE");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
	 printf("OPEN_FILE\n");

     // obtain file size:
     fseek (pFile , 0 , SEEK_END);
     lSize = ftell (pFile);
     rewind (pFile);
	
     // allocate memory to contain the whole file:
     content = (char*) malloc (sizeof(char)*lSize);
     if (content == NULL) 
	 {
		 printf("ERROR_MEMORY");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
     printf("MEMORY_ALLOCATED\t \"%d\" \n",&lSize);
     // copy the file into the buffer:
     result = fread (content,1,lSize,pFile);
     if (result != lSize) 
	 {
		 printf("ERROR_SIZE");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
     printf("SIZE_OK\n");

     // terminate
     fclose (pFile);
     printf("FILE_CLOSE\n");
     //allocate memory to contain the whole file + HEADER
     buffer = (char*) malloc (sizeof(char)*lSize + 2048);

     //print header
     sprintf(hdrs,"Content-Type: multipart/form-data; boundary=%s",boundary);
     sprintf(buffer,"%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename);
     sprintf(buffer,"%sContent-Type: %s\r\n",buffer,type);
	 sprintf(buffer,"%s%s",buffer,content);
     sprintf(buffer,"%s--%s--\r\n",buffer,boundary);



     //Open internet connection
     HINTERNET hSession = InternetOpen("WINDOWS",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
     if(hSession==NULL) 
	 {
		 printf("ERROR_INTERNET_OPEN");
		 getchar();
		 return ERROR_OPEN_FILE;
	 }
     printf("INTERNET_OPENED\n");

     HINTERNET hConnect = InternetConnect(hSession, iaddr,INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
     if(hConnect==NULL) 
	 {
		 printf("ERROR_INTERNET_CONN");
		 getchar();
		 return ERROR_INTERNET_CONN;
	 }
	 printf("INTERNET_CONNECTED\n");

     HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T(url),NULL, NULL, NULL,INTERNET_FLAG_RELOAD, 1);
     if(hRequest==NULL) 
	  {
		 printf("ERROR_INTERNET_REQ");
		 getchar();
		
	 }
	 printf("INTERNET_REQ_OPEN\n");

     BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), buffer, strlen(buffer));
    	
     if(!sent) 
	 {
		 printf("ERROR_INTERNET_SEND");
		 getchar();
		 return ERROR_INTERNET_CONN;
	 }
	 printf("INTERNET_SEND_OK\n");

     //close any valid internet-handles
     InternetCloseHandle(hSession);
     InternetCloseHandle(hConnect);
     InternetCloseHandle(hRequest);
    
	 
     
	 getchar();
	 return 0;
  }

推荐答案

只是浏览你的来源(因为你没有描述出现了什么问题),以下几行出来作为一个简单的错误:

Just skimming over your source (as you didn't describe what went wrong) the following lines stick out as a simple error:
sprintf(buffer,"%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n",boundary,nameForm,filename);
 sprintf(buffer,"%sContent-Type: %s\r\n",buffer,type);
 sprintf(buffer,"%s%s",buffer,content);
 sprintf(buffer,"%s--%s--\r\n",buffer,boundary);



第二个和第三个sprintf不附加到你已写的内容,但覆盖它!要么使用单个sprintf来完成所有操作,要么保留指向下一个sprintf的指针:


The second and third sprintf do not append to what you already have written, but overwrite it! Either use a single sprintf to do it all, or keep a pointer to where the next sprintf should go:

char* p = buffer;
p += sprintf (p, "....", ....);
p += sprintf (p, "....", ....);



当然,上面的代码只是为了清晰起见。在实际应用程序中,您需要进行一些错误检查,因为sprintf可能会在出现错误时返回-1。


Of course, the above code is just for clarity. In a real application you would need to do some error checking as sprintf may return -1 in case of an error.


这篇关于如何在c ++中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆