僅供參考,這裏是我試圖在iOS中做/ Objective-C的使用AFNetworking以文本和多張圖片上傳到谷歌Blob存儲
public static void saveTextsAndImagesOnServer(List<byte[]> images, long someID1, String servingUrl, boolean someFlag)
throws ClientProtocolException, IOException {
Log.d(TAG, "saveTextsAndImagesOnServer started ");
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(servingUrl);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
AdditionData extr = AdditionData.getInstance();
reqEntity.addPart("red", new ByteArrayBody(("" + extr.getred()).getBytes(), "red"));
reqEntity.addPart("yellow", new ByteArrayBody(extr.getyellow.getBytes(), "yellow"));
reqEntity.addPart("green", new ByteArrayBody(extr.getgreen().getBytes(), "green"));
reqEntity.addPart("blue", new ByteArrayBody((extr.getblue()).getBytes(), "blue"));
reqEntity.addPart("someID1", new ByteArrayBody(("" + someID1).getBytes(), "someID1"));
if (someFlag) {
reqEntity.addPart("someFlag", new ByteArrayBody("true".getBytes(), "someFlag"));
}
int i = 0;
for (byte[] img : images) {
ByteArrayBody image = new ByteArrayBody(img, "img" + i++ + ".png");
reqEntity.addPart("image", image);
}
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
Log.d(TAG, "saveTextsAndImagesOnServer ended with response " + response.toString());
}
總結工作的Android/Java版本:基本上我我能夠將圖像和伴隨的元數據發送到blobstore。例如,元數據可幫助我識別發送圖像的人員。
現在試圖在iOS中做同樣的事情我寫了下面的代碼。但無論出於何種原因,元數據未保存在Blobstore中。我嘗試了一堆不同的東西,但我仍然沒有得到任何東西。我嘗試將我的元數據字符串更改爲base-64,但這不起作用。
的IOS代碼
-(void)postMultipartToServer
{
if (!self.destinationUrl) {
return;
}
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSLog(@"IS dictionary empty? %@", self.textDictionary);
[manager POST:self.destinationUrl
parameters:self.textDictionary
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if ([self.imageDictionaries count]>0)
for (NSDictionary *imgDic in self.imageDictionaries) {
[formData appendPartWithFileData:UIImagePNGRepresentation([imgDic objectForKey:@"image"])
name:[imgDic objectForKey:@"name"]//@"image"
fileName:[imgDic objectForKey:@"fileName"]//@"image.png"
mimeType:[imgDic objectForKey:@"mimeType"]//@"image/png"
];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
}
這裏是字典
NSDictionary *textDictionary = @{
@"yellow」:self. yellow,
@"red":self. red,
@"green」:self.green,
@「blue」:self. blue,
@「spouse」:self.spouse,
@"isFamouse」:@」true」};//you get the idea
我在哪裏?
鑑於此設計適用於Android,我知道這可能是特別有可能的,因爲在涉及到語言時,http應該是不可知的。所以問題被簡化爲將字符串從iOS保存到Google blobstore。
我開始賞金,希望有人能幫我解決這個問題(也許是谷歌blobstore-iOS專家)。我也注意到其他人已經提出了相同的問題,但沒有回答。其他方面我也問過這樣一個問題:
AFHTTPRequestOperationManager post multi-part request not working
How to convert NSString to UIImage for appengine blobstore
:目的地是谷歌Blob存儲(如果這事項以某種方式) – learner
這個網站是不是一個免費的代碼翻譯服務。先做自己的工作。如果您遇到特定問題,請發佈相關代碼並解釋您的問題。 – rmaddy
@rmaddy我已經這樣做了,現在我開始賞金了。用於java代碼參考的 – learner