2017-04-13 179 views
0

它是我第一次上傳圖片 的陣列我使用Alamofire和上傳成功 ,但我發現它重複第一圖像上傳多個圖像

let selectedImages = NSMutableArray.init() 
    for img in Photos {     // photos = [UIImage]() 
     selectedImages.add(UIImageJPEGRepresentation(img, 0.1)!) 
    } 
    var s:String = mainTitleTextfield.text! 
    if s == "" || s == " " { 
     let f = DateFormatter() 
     f.dateFormat = "MMM d, yyyy" 
     let date = Date() 
     s = f.string(from: date) 
    } 
    s = s.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)! 
    let url = serverURL + "postPhotosToClasses/?AlbumName=" + s + "&IsGroup=\(false)" 
    HUD.show(.label("uploading")) 
    Alamofire.upload(multipartFormData: { multipartFormData in 

     for i in 0..<selectedImages.count { 
      multipartFormData.append(selectedImages[i] as! Data, withName: "file",fileName: "image.jpg", mimeType: "image/jpeg") 
     } 
     multipartFormData.append(selectedImages[1] as! Data, withName: "file",fileName: "image.jpg", mimeType: "image/jpeg") 
     _ = 3 

    }, to: url,method:.post, 
     headers:["UserID":"\(currentTeacher.ID)","Ids":arr], encodingCompletion: { encodingResult in 
     switch encodingResult { 
     case .success(let upload, _, _): 
      upload 
       .validate() 
       .responseJSON { response in 
        switch response.result { 
        case .success(let value): 
         print("responseObject: \(value)") 
         HUD.flash(.success, delay: 1.0) 
        case .failure(let responseError): 
         print("responseError: \(responseError)") 

        } 
      } 
     case .failure(let encodingError): 
      print("encodingError: \(encodingError)") 

     } 
    }); 

} 

可能是使用問題的循環中multipartFormData

+0

什麼是'multipartFormData.append ...'_after_ for循環? – shallowThought

+0

我添加這段代碼來測試foor循環中的問題 但它遇到同樣的問題並上傳selectedimage [0] –

回答

0

問題追加在withName 當變化withName:"file"更換

for i in 0..<selectedImages.count { 
    multipartFormData.append(selectedImages[i] as! Data, withName: "file",fileName: "image.jpg", mimeType: "image/jpeg") 
    } 

這一行 到withName:"file\(i)"

for i in 0..<selectedImages.count { 
    multipartFormData.append(selectedImages[i] as! Data, withName: "file\(i)",fileName: "image.jpg", mimeType: "image/jpeg") 
    }