// webteam代碼如何將iOS中的圖像數組作爲多部分請求進行上傳?
變種hoardingjson = { _id:ID, populationincomegroup:incomeGroupArray, availablebydate:availDate, 標題:標題, 描述:降序, onemonthprice:oneMprice, threemonthsprice: threeMprice, sixmonthsprice:sixMprice, oneyearprice:oneYrprice, 地區:本地, streetname:streetName, trafficdensity:trafficDensity, populationcategory:populati onDensity, OWNERNAME:OWNERNAME, 移動:移動, leaseowner:currenrOwnerName, 長度:hoardingWidth, 廣度:hoardingHeight, 緯度:緯度, 經度:LON, registereddate:currentTimestamp, CREATEDATE: 「」, updatedate :「」, status:「」, ownerid:ownerid };
var form_data = new FormData();
var imageCount = document.getElementById("editImages").files.length;
for (i = 0; i < imageCount; i++) {
form_data.append("file", document.getElementById("editImages").files[0]);
}
var xhr = new XMLHttpRequest();
xhr.open("POST", server_url + "UpdateHoarding", true);
xhr.setRequestHeader("hoardingjson", JSON.stringify(hoardingjson));
xhr.send(form_data);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
success = true;
$("#updateHoardingModal").modal("hide");
alert("Hoarding is updated successfully");
location.href = "home.jsp";
}
}
}
奧斯代碼:
的NSString * jsosSt = [自strOfthedata];
UIImage *imageOne = [UIImage imageNamed:@"edit.png"];
NSData *imageData1 = UIImageJPEGRepresentation(imageOne,0.6f);
NSString *fileName1 = [NSString stringWithFormat:@"%ld%c%c.jpeg", (long)[[NSDate date] timeIntervalSince1970], arc4random_uniform(26) + 'a', arc4random_uniform(26) + 'a'];
的NSDictionary * PARAMS = @ {@ 「令牌」:appdeligate.userInf.tokenId,@ 「hoardingjson」:[自strOfthedata]};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"URL/CreateHoarding" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:[NSURL fileURLWithPath:savedImagePath] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSMutableString *strParams = [[NSMutableString alloc]init];
for (NSString *key in [params allKeys]) {
[strParams appendFormat:@"%@=%@", key, params[key]];
[strParams appendString:@"&"];
}
[strParams deleteCharactersInRange:NSMakeRange([strParams length]-1, 1)];
[request setHTTPBody:[strParams dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
dispatch_async(dispatch_get_main_queue(), ^{
//Update the progress view
[progressView setProgress:uploadProgress.fractionCompleted];
NSLog(@"%f",uploadProgress.fractionCompleted);
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
} else {
NSLog(@"%@ %@", response, responseObject);
}
}];
[uploadTask resume];
有你看看這個[問題](http://stackoverflow.com/questions/37941306/download-one-file-at-a-time-using-afnetworking/)則要求下載你需要實現相同的只是爲了上傳 –
你使用AFNetwork,正確 –
是Afnetworking 3,但圖像形式部分沒有收到,但json正在交付,而我正在發送[請求setValue:@「application/x-www- form-urlencoded「forHTTPHeaderField:@」content-type「];即使json也得到了我需要檢查的服務器正確接收圖像 –