当前位置:首页 » 文件管理 » 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)

热点内容
qq密码修改什么比较简单 发布:2025-06-24 03:07:10 浏览:971
按键精灵什么是安卓系统 发布:2025-06-24 03:02:22 浏览:539
php非线程安全 发布:2025-06-24 02:51:09 浏览:65
网页禁止缓存 发布: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
海康网络存储服务器挂载多台电脑 发布:2025-06-24 01:19:00 浏览:618