×
Namespaces

Variants
Actions

Symbian C++: Multipart/form-data

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 06 May 2010 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

下列代码可以用来上传任何文件,图片,文本,视频等,都可以作为一个二进制文件发送给服务器

这里的HTTPEngine需要能实现其父类MHTTPDataSupplier的接口,下列步骤可以用来实现上传过程:

TBool GetNextDataPart(TPtrC8& aDataPart);
void ReleaseData();
TInt Reset();
TInt OverallDataSize();

用来做post的content-type应为:

_LIT8(KPostContentType, "multipart/form-data; boundary=AaB03x");

MHFRunLEGotResponseBodyData下应为:

MHTTPDataSupplier* dataSupplier = aTransaction.Response().Body();
TPtrC8 ptr;
dataSupplier->GetNextDataPart(ptr);
 
 
// Convert to 16-bit descriptor
HBufC* buf = HBufC::NewLC(ptr.Length());
buf->Des().Copy(ptr);
 
// Append to iResponseBuffer
if (iResponseBuffer==NULL)
{
iResponseBuffer = buf->AllocL();
}
else
{
iResponseBuffer = iResponseBuffer->ReAllocL(iResponseBuffer->Length()+buf->Length());
iResponseBuffer->Des().Append(*buf);
}
 
// Release buf
CleanupStack::PopAndDestroy(buf);
 
if(!iDataAvailable)
{
iRunning=EFalse;
iTransaction.Close();
}
else
{
dataSupplier->ReleaseData();
}


POST代码 获得你要发送文件的二进制数据

RFs aFs;
User::LeaveIfError(aFs.Connect());
CleanupClosePushL(aFs);
 
RFile aFile;
 
User::LeaveIfError(aFile.Open(aFs, iFileName, EFileShareAny));
 
TInt aSize;
User::LeaveIfError(aFile.Size(aSize));
 
iPostDataImage = HBufC8::NewL(aSize);
TPtr8 aPtr = iPostDataImage->Des();
 
//Obviously we have to read in the data to the iPostDataImage (ray)
aFile.Read(0, aPtr, aSize);
 
aFile.Close();
aFs.Close();
CleanupStack::PopAndDestroy(&aFs);

使用EFileShareAny以确保文件可以被其他文件打开。将此文件读取到 iPostDataImage作为一个二进制文件

现在生成上传格式

_LIT8(KDataStart,"--AaB03x");
_LIT8(KCrlf,"\r\n");
_LIT8(KContent,"Content-Disposition: form-data; name='userfile'; filename='");
_LIT8(KFileCompletion,"'");
 
_LIT(KContent2,"Content-Type: image/gif");
_LIT(KContent3,"Content-Transfer-Encoding: binary");
_LIT8(KDataEnd,"--AaB03x--");
 
iPostData = HBufC8::NewL(650+aPtr.Length());
 
 
TPtr8 iPostDataPtr = iPostData->Des();
iPostDataPtr.Zero();
 
iPostDataPtr.Append(KCrlf);
iPostDataPtr.Append(KDataStart);
iPostDataPtr.Append(KCrlf);
iPostDataPtr.Append(KContent);
iPostDataPtr.Append(aFname);
iPostDataPtr.Append(KFileCompletion);
iPostDataPtr.Append(KCrlf);
iPostDataPtr.Append(KContent2);
iPostDataPtr.Append(KCrlf);
iPostDataPtr.Append(KContent3);
iPostDataPtr.Append(KCrlf);
iPostDataPtr.Append(KCrlf);
iPostDataPtr.Append(aPtr); //the file in binary
iPostDataPtr.Append(KCrlf);
iPostDataPtr.Append(KDataEnd);
iPostDataPtr.Append(KCrlf);


提交发送

RHTTPHeaders hdr = iTransaction.Request().GetHeaderCollection();
AddHeaderL(hdr, HTTP::EUserAgent, KUserAgent);
AddHeaderL(hdr, HTTP::EAccept, KAccept);
AddHeaderL(hdr, HTTP::EContentType, KPostContentType);
 
MHTTPDataSupplier* dataSupplier = this;
iTransaction.Request().SetBody(*dataSupplier);
 
iTransaction.SubmitL();

完成DataSupplier函数

TBool CHTTPExampleEngine::GetNextDataPart(TPtrC8& aDataPart)
{
TBool retVal = EFalse;
aDataPart.Set(iPostData->Des());
retVal = (aDataPart.Length() == 0);
iDataAvailable=retVal;
return retVal;
}
 
TInt CHTTPExampleEngine::OverallDataSize()
{
if(iPostData)
return iPostData->Length();
else
return KErrNotFound ;
}
 
void CHTTPExampleEngine::ReleaseData()
{
if(iDataAvailable)
iTransaction.NotifyNewRequestBodyPartL();
}

至此可以传送任何二进制数据到服务器上

This page was last modified on 15 December 2011, at 09:16.
121 page views in the last 30 days.