2017-09-12 76 views
0

如何上傳圖像陣列多部分身體把它隨着莫亞Swift3上傳圖片多部分隨着莫亞斯威夫特3全例如

我的代碼

var multipartBody: [MultipartFormData]? { 
    switch self { 
    case .registerWith(let registrationObject): 
     if let profileImageData = registrationObject.profileImageData{ 
     return [MultipartFormData(provider: .data(profileImageData), name: "profile_picture", fileName: "\(registrationObject.phone).jpg", 
      mimeType: "image/jpeg")] 
     } 
     return nil 
    default: 
     return nil 
    } 
} 

var task: Task { 
    switch self { 
    case .registerWith: 
     if let multipartBody = multipartBody { 
      return .upload(.multipart(multipartBody)) 
     }else{ 
      return .request 
     } 
    default: 
     return .request 
    } 
} 

,但我需要發送的imageData中體鍵「profileImage」

回答

0

您需要將「name:」profile_picture「」更改爲「name:」profileImage「」, 試試這個。

0

要使用莫亞

1提檔多部分上傳的圖像要在MultipartFormData陣列上傳

// put files in MultipartFormData Arry 
var multipartBody: [MultipartFormData]? { 
    switch self { 
      case .registerWith(let registrationObject): 
       let profileImageData:Data = registrationObject.profileImageData as Data 
       return [MultipartFormData(provider: .data(profileImageData), 
      name: "profile_picture", fileName: "fileName", mimeType: "image/jpeg")] 
       default: 
       return nil 
     } 
} 

2-添加multipartBody提供商的方法的要求的身體,你用得着這種方法的

var parameters: [String: Any]? { 
    switch self { 
      case .registerWith(let registrationObject): 
      var body = registrationObject.toJSON()! 
      // add the file in body 
      body["profile_picture"] = multipartBody?[0].provider 
      return body 
    } 
} 

3-化妝任務類型上傳

var task: Task { 
     switch self { 
      case .registerWith: 
       // multipartBody that in First step 
      return .upload(.multipart(multipartBody)) 
     } 
} 
0

聽到的是代碼使用Moya的版本10.0.2

//MARK:- MultipartData - 
var multipartBody :[Moya.MultipartFormData]?{ 
    switch self { 
    case .addUser(let userInfo): 

     //other parameters need to convert to data 
     let vName = userInfo.name 
     let vNameData = Moya.MultipartFormData(provider: MultipartFormData.FormDataProvider.data(vName.data(using: .utf8)!), name: "vName") 

     //convert your image to data 
     guard let data = UIImageJPEGRepresentation(userInfo.profilePic, 1.0) else { return nil } 

     return [Moya.MultipartFormData(provider: MultipartFormData.FormDataProvider.data(data), name: "vAvtarName", fileName: "photo.jpg", mimeType: "image/jpeg"),vNameData] 
    default: 
     return [] 
    } 
} 

var task: Task { 
    switch self { 
     case .addUser(let userInfo): 
     return .uploadMultipart(self.multipartBody!) 
    } 
} 
上傳圖像