下面是網上的一個HTTP上傳函式:
UploadFile(LPCTSTR strURL, //負責接收上傳操作的頁面的URL
LPCTSTR strLocalFileName) //待上傳的本地檔案路徑
{
ASSERT(strURL != NULL && strLocalFileName != NULL);
BOOL bResult = FALSE;
DWORD dwType = 0;
CString strServer;
CString strObject;
INTERNET_PORT wPort = 0;
DWORD dwFileLength = 0;
char * pFileBuff = NULL;
CHttpConnection * pHC = NULL;
CHttpFile * pHF = NULL;
CInternetSession cis;
bResult = AfxParseURL(strURL, dwType, strServer, strObject, wPort);
if(!bResult)
return FALSE;
CFile file;
try
{
if(!file.Open(strLocalFileName, CFile::shareDenyNone | CFile::modeRead))
return FALSE;
dwFileLength = file.GetLength();
if(dwFileLength <= 0)
return FALSE;
pFileBuff = new char[dwFileLength];
memset(pFileBuff, 0, sizeof(char) * dwFileLength);
file.Read(pFileBuff, dwFileLength);
const int nTimeOut = 5000;
cis.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, nTimeOut); //聯接超時設定
cis.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1); //重試1次
pHC = cis.GetHttpConnection(strServer, wPort); //取得一個Http聯接
pHF = pHC->OpenRequest(CHttpConnection::HTTP_VERB_POST, strObject);
if(!pHF->SendRequest(NULL, 0, pFileBuff, dwFileLength))
{
delete[]pFileBuff;
pFileBuff = NULL;
pHF->Close();
pHC->Close();
cis.Close();
return FALSE;
}
DWORD dwStateCode = 0;
pHF->QueryInfoStatusCode(dwStateCode);
if(dwStateCode == HTTP_STATUS_OK)
bResult = TRUE;
}
catch(CInternetException * pEx)
{
char sz[256] = "";
pEx->GetErrorMessage(sz, 25);
CString str;
str.Format("InternetException occur!\r\n%s", sz);
AfxMessageBox(str);
}
catch(CFileException& fe)
{
CString str;
str.Format("FileException occur!\r\n%d", fe.m_lOsError);
AfxMessageBox(str);
}
catch()
{
DWORD dwError = GetLastError();
CString str;
str.Format("Unknow Exception occur!\r\n%d", dwError);
AfxMessageBox(str);
}
delete[]pFileBuff;
pFileBuff = NULL;
file.Close();
pHF->Close();
pHC->Close();
cis.Close();
return bResult;
}
然后我將D盤上一張圖片上傳,發現函式回傳是真,Web服務器上卻沒有東西。
下面是我的呼叫:
UploadFile(“http://192.168.15.30:8090/user”,"D://his.bmp" )
然后用
UploadFile(“http://192.168.15.30:8090”,"D://his.bmp" )
還是一樣失敗!
uj5u.com熱心網友回復:
服務端接收圖片的代碼怎么寫的?轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/116812.html
標籤:基礎類
上一篇:如何全域隱藏滑鼠!!
下一篇:求助!新手不知道怎么回事
