當前位置:首頁 » 文件管理 » ios附件上傳

ios附件上傳

發布時間: 2025-06-24 00:11:33

Ⅰ ios怎樣使用multipart from方式上傳網路數據

在網路編程過程中需要向伺服器上傳文件。Multipart/form-data是上傳文件的一種方式。


Multipart/form-data其實就是瀏覽器用表單上傳文件的方式。最常見的情境是:在寫郵件時,向郵件後添加附件,附件通常使用表單添加,也就是用multipart/form-data格式上傳到伺服器。


表單形式上傳附件


具體的步驟是怎樣的呢?


首先,客戶端和伺服器建立連接(TCP協議)。


第二,客戶端可以向伺服器端發送數據。因為上傳文件實質上也是向伺服器端發送請求。


第三,客戶端按照符合「multipart/form-data」的格式向伺服器端發送數據。


Multipart/form-data的格式是怎樣的呢?


既然Multipart/form-data格式就是瀏覽器用表單提交數據的格式,我們就來看看文件經過瀏覽器編碼後是什麼樣子。



//---------------------------------------

// this is the demo code of using multipart/form-data to upload text and
photos.

// -use WinInet APIs.

//

//

// connection handlers.

//

HRESULT hr;

HINTERNET m_hOpen;

HINTERNET m_hConnect;

HINTERNET m_hRequest;

//

// make connection.

//

...

//

// form the content.

//

std::wstring strBoundary = std::wstring(L"------------------");

std::wstring wstrHeader(L"Content-Type: multipart/form-data, boundary=");

wstrHeader += strBoundary;

HttpAddRequestHeaders(m_hRequest, wstrHeader.c_str(),
DWORD(wstrHeader.size()), HTTP_ADDREQ_FLAG_ADD);

//

// "std::wstring strPhotoPath" is the name of photo to upload.

//

//

// uploaded photo form-part begin.

//

std::wstring strMultipartFirst(L"--");

strMultipartFirst += strBoundary;

strMultipartFirst += L" Content-Disposition: form-data; name="pic";
filename=";

strMultipartFirst += L""" + strPhotoPath + L""";

strMultipartFirst += L" Content-Type: image/jpeg ";

//

// "std::wstring strTextContent" is the text to uploaded.

//

//

// uploaded text form-part begin.

//

std::wstring strMultipartInter(L" --");

strMultipartInter += strBoundary;

strMultipartInter += L" Content-Disposition: form-data;
name="status" ";

std::wstring
wstrPostDataUrlEncode(CEncodeTool::Encode_Url(strTextContent));

// add text content to send.

strMultipartInter += wstrPostDataUrlEncode;

std::wstring strMultipartEnd(L" --");

strMultipartEnd += strBoundary;

strMultipartEnd += L"-- ";

//

// open photo file.

//

// ws2s(std::wstring)

// -transform "strPhotopath" from unicode to ansi.

std::ifstream *pstdofsPicInput = new std::ifstream;

pstdofsPicInput->open((ws2s(strPhotoPath)).c_str(),
std::ios::binary|std::ios::in);

pstdofsPicInput->seekg(0, std::ios::end);

int nFileSize = pstdofsPicInput->tellg();

if(nPicFileLen == 0)

{

return E_ACCESSDENIED;

}

char *pchPicFileBuf = NULL;

try

{

pchPicFileBuf = new char[nPicFileLen];

}

catch(std::bad_alloc)

{

hr = E_FAIL;

}

if(FAILED(hr))

{

return hr;

}

pstdofsPicInput->seekg(0, std::ios::beg);

pstdofsPicInput->read(pchPicFileBuf, nPicFileLen);

if(pstdofsPicInput->bad())

{

pstdofsPicInput->close();

hr = E_FAIL;

}

delete pstdofsPicInput;

if(FAILED(hr))

{

return hr;

}

// Calculate the length of data to send.

std::string straMultipartFirst = CEncodeTool::ws2s(strMultipartFirst);

std::string straMultipartInter = CEncodeTool::ws2s(strMultipartInter);

std::string straMultipartEnd = CEncodeTool::ws2s(strMultipartEnd);

int cSendBufLen = straMultipartFirst.size() + nPicFileLen +
straMultipartInter.size() + straMultipartEnd.size();

// Allocate the buffer to temporary store the data to send.

PCHAR pchSendBuf = new CHAR[cSendBufLen];

memcpy(pchSendBuf, straMultipartFirst.c_str(),
straMultipartFirst.size());

memcpy(pchSendBuf + straMultipartFirst.size(), (const char *)pchPicFileBuf,
nPicFileLen);

memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen,
straMultipartInter.c_str(), straMultipartInter.size());

memcpy(pchSendBuf + straMultipartFirst.size() + nPicFileLen +
straMultipartInter.size(), straMultipartEnd.c_str(),
straMultipartEnd.size());

//

// send the request data.

//

HttpSendRequest(m_hRequest, NULL, 0, (LPVOID)pchSendBuf, cSendBufLen)

熱點內容
xutils上傳多文件 發布:2025-06-24 03:55:21 瀏覽:379
qq密碼修改什麼比較簡單 發布:2025-06-24 03:07:10 瀏覽:974
按鍵精靈什麼是安卓系統 發布:2025-06-24 03:02:22 瀏覽:542
php非線程安全 發布:2025-06-24 02:51:09 瀏覽:67
網頁禁止緩存 發布:2025-06-24 02:15:34 瀏覽:588
電腦配置低如何調apex 發布:2025-06-24 02:05:45 瀏覽:607
python代碼顏色 發布:2025-06-24 01:53:53 瀏覽:88
discuz資料庫查詢 發布:2025-06-24 01:44:43 瀏覽:915
圖案密碼從中間一點開始都有多少 發布:2025-06-24 01:38:20 瀏覽:375
配置最低的手機版吃雞游戲是哪個 發布:2025-06-24 01:24:24 瀏覽:720