ckeditor加ActiveXUpImg的ocx源代码

之前发表过一篇ckeditor上传文件的博客,其中用到一个插件,是用delphi开发的ActiveX,先将ocx的代码发上来,代码很少,不过需要找工具生成一下,由于我的代码也是在公司里,公司里限制,ocx、dll文件无法拷出上传,只能贴源码了。 插件在jsp和aspx里的接收流是一样的,具体可参考以前的aspx的接受代码实例。

公司里的同事已经测试通过,在项目里正式使用了

// UpImage.cpp : Implementation of CActiveXUpImgApp and DLL registration.

#include "stdafx.h"
#include "ActiveXUpImg.h"
#include "UpImage.h"

#include "string"
#include "iostream"
#include "stdio.h"
#include "Wininet.h"

#pragma comment(lib, "Wininet.lib")
#define BUF_SIZE 1024
using namespace std;

/
//

STDMETHODIMP UpImage::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IUpImage,
	};

	for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP UpImage::UploadImg2Server(long a,long b)
{
	// TODO: Add your implementation code here
char* Ip="192.168.0.133";
	int port=3011;
	char* upFile="c:/kinectsensor.jpg";
	HINTERNET hSession=NULL, hConnect=NULL, hRequest=NULL;
	 hSession = InternetOpen("test", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
	 hConnect = InternetConnect(hSession, Ip, port,//INTERNET_DEFAULT_HTTP_PORT,  
	  NULL, NULL,INTERNET_SERVICE_HTTP, INTERNET_FLAG_NO_CACHE_WRITE, NULL);
	 INTERNET_BUFFERS BufferIn = {0};
	 DWORD dwBytesRead;
	 DWORD dwBytesWritten;
	 BYTE pBuffer[1024]; // Read from file in 1K chunks
	 BOOL bRead, bRet;
	 BufferIn.dwStructSize = sizeof(INTERNET_BUFFERS);
	 hRequest = HttpOpenRequest(hConnect, "POST", "xml_convert", NULL, NULL, NULL, 0, 0);  // xml_convert请求实体
	 if (!hRequest)
	 {
		   printf("Failed to open request handle: %lu\n", GetLastError ());
		   return FALSE;
	 }
	 HANDLE hFile = CreateFile(upFile, GENERIC_READ, FILE_SHARE_READ,
	  NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	 if (hFile == INVALID_HANDLE_VALUE)
	 {
		  printf("\nFailed to open local file %s.", upFile);
		  return FALSE;
	 }
	 BufferIn.dwBufferTotal = GetFileSize(hFile, NULL);
	 printf ("File size is %d\n", BufferIn.dwBufferTotal );
	 if(!HttpSendRequestEx(hRequest, &BufferIn, NULL, HSR_INITIATE, 0))
	 {
		  printf("Error on HttpSendRequestEx %lu\n",GetLastError());
		  return FALSE;
	 }
	 DWORD sum = 0;
	 do
	 {
	  if (!(bRead = ReadFile(hFile, pBuffer, sizeof(pBuffer), &dwBytesRead, NULL)))
	  {
		   printf ("\nReadFile failed on buffer %lu.",GetLastError());
		   break;
	  }
	  if (!(bRet=InternetWriteFile( hRequest, pBuffer, dwBytesRead, &dwBytesWritten)))
	  {
		  printf ("\nInternetWriteFile failed %lu", GetLastError());
		  break;
	  }
		   sum += dwBytesWritten;
	 }
	 while (dwBytesRead == sizeof(pBuffer)) ;

		   CloseHandle(hFile);
		  printf ("Actual written bytes: %d\n", sum);
	 if(!HttpEndRequest(hRequest, NULL, 0, 0))
	 {
		   printf( "Error on HttpEndRequest %lu \n", GetLastError());
		   return FALSE;
	 }

	 int contextLengthId = HTTP_QUERY_CONTENT_LENGTH;   
	 int statusCodeId = HTTP_QUERY_STATUS_CODE;   
	 int statusTextId = HTTP_QUERY_STATUS_TEXT;  
	 char szBuf[BUF_SIZE] = {0};   
	 DWORD dwSize = BUF_SIZE;
 

	 char pcBuffer[BUF_SIZE];
	 DWORD dwByteRead;

	 //printf("\n\n服务端返回如下:\n");
	 do
	 {    
			dwBytesRead = 0;
		   if(InternetReadFile(hRequest, pcBuffer, BUF_SIZE - 1, &dwByteRead))
		  {
				pcBuffer[dwByteRead] = 0x00; // Null-terminate buffer
				printf("%s", pcBuffer);
				memset(pcBuffer, 0, BUF_SIZE);
		 }
		else
		{
			 printf("InternetReadFile failed\n");
		 }
	 }while(dwByteRead>0);

	 /*if (HttpQueryInfo(hRequest, statusTextId, szBuf, &dwSize, 0))
	 {
			szBuf[dwSize] = 0;   
			printf("Status text:[%s]\n", szBuf);   

	 }*/

		 //cout<<"上传成功"<<endl;
		 return TRUE;
	return S_OK;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值