文件类型用于将二进制数据发布到各种 API 调用中。每个包含一个类型 ⁇ file ⁇ 的参数的 API 调用都必须符合以下要求:
-
必须使用 HTTP POST 方法。
-
必须形成HTML规范 ⁇ 中描述的多部分/表格数据请求。
示例
Java HttpClient
HttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(webServerUrl); 多部分实体请求实体 = 新的多部分实体(HttpMultipartMode.BROWSER_COMPATIBLE, null, Charset.forName(“UTF-8”)); StringBody taskPart; StringBody formatPart; StringBody 保存到TransMemoryPart; StringBody setCompletedPart; ByteArrayBody fileContentPart; //... 用数据填充变量... // reqEntity.addPart("task", taskPart); reqEntity.addPart("format", formatPart); reqEntity.addPart("saveToTransMemory", saveToTransMemoryPart); reqEntity.addPart("setCompleted", setCompletedPart); reqEntity.addPart("bilingualFile", fileContentPart); httpPost.setEntity(reqEntity);
C++ Qt
QHttpMultiPart* multiPartData = new QHttpMultiPart( QHttpMultiPart::FormDataType ); QBuffer* buffer = new QBuffer( multiPartData ); buffer->open( QIODevice::WriteOnly ); //... 用数据填充缓冲区... // writer.writeTo( buffer ); buffer->close(); QHttpPart filePart; filePart.setHeader( QNetworkRequest::ContentTypeHeader, QVariant( "application/octet-stream" ) ); filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QString("form-data; name =\"双语文件\"; filename =\" ) + model->getFileInfos()[0].original + "\" ) ); buffer->open( QIODevice::ReadOnly ); filePart.setBodyDevice( buffer ); multiPartData->append( filePart );