Symbian 中上传文件|Symbian C++ : Multipart/form-data
来源:互联网 发布:淘宝上杨氏鼻炎膏 编辑:程序博客网 时间:2024/05/21 06:34
The following code can be used to upload any file, image, text , video etc.. as a binary file to a server.
The HTTPEngine should have MHTTPDataSupplier as its parent class, as the following functions need to be implemented for the upload process
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TBool GetNextDataPart(TPtrC8& aDataPart);
void ReleaseData();
TInt Reset();
TInt OverallDataSize();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
The content-type for the post should be
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
_LIT8(KPostContentType, "multipart/form-data; boundary=AaB03x");
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
In the MHFRunL the case EGotResponseBodyData should do this
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 code Get the file you want to upload in binary format
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Using EFileShareAny ensures that if the file can be opened by any other application. The file is read into iPostDataImage as a binary file.
Now create the upload format
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
_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);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Submit the transaction
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Implement DataSupplier functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Any binary file can be uploaded with this upload code.
Retrieved from "http://wiki.forum.nokia.com/index.php/Symbian_C%2B%2B_:_Multipart/form-data"
- Symbian 中上传文件|Symbian C++ : Multipart/form-data
- multipart/form-data 上传文件
- 上传文件multipart form-data boundary 说明
- Http 上传文件之multipart form-data
- Multipart/form-data POST文件上传详解
- Multipart/form-data POST文件上传详解
- 文件上传 enctype multipart/form-data
- Multipart/form-data POST文件上传详解
- python 发送上传文件multipart/form-data
- Multipart/form-data POST文件上传详解
- Multipart/form-data POST文件上传分析
- Multipart/form-data POST文件上传详解
- multipart/form-data格式上传文件
- 使用multipart/form-data上传文件
- Multipart/form-data POST文件上传详解
- 使用multipart/form-data上传文件
- Multipart/form-data POST文件上传详解
- Multipart/form-data文件上传简介
- 母婴用品加盟店皇家贝贝,全国的王牌
- 简单反汇编之注册机编写
- 回调方式的简单实例
- 项目的开发
- 什么是服务原语, 服务原语和协议的区别!
- Symbian 中上传文件|Symbian C++ : Multipart/form-data
- ARM 程序设计优化策略与技术
- 病来如山倒
- java中的访问权限
- javascript 禁止复制网页
- Google對我的博客抓取
- SEO外链优化篇
- jQuery异步查询
- WCF一个小经验分享一下(元数据包含无法解析的引用)