2017-03-06 145 views
0

我正在使用Alamofire將數據發佈到服務器。我想用一些其他參數以數據形式上傳到服務器的圖像。在Alamofire中,我使用multipartFormData方法來發布所有參數和圖像。服務器需要將數據與參數JSON格式如下所示:在Swift中使用Alamofire使用參數上傳圖片

{"product_name": "almondsfdsfsdf", 
"product_price": "400", 
"product_img": image.jpg} 

我想,但它給了我一個失敗的響應。這裏是我用alamofire快速完成的工作代碼:

let productName = itemNameTF.text! 
let productPrice = itemPriceTF.text! 
let productImage:UIImage = itemImage.image! 
let url = "URL" 
let parameter = ["product_name": productName, "product_price": productPrice] 
let headers : HTTPHeaders = ["Content-Type": "application/json","Authorization" : "Token abcd"] 
Alamofire.upload(multipartFormData: { 
      multipartFormData in 
      if let imageData = UIImageJPEGRepresentation(productImage, 0.5){ 
       multipartFormData.append(imageData, withName: "image", fileName: "file.png", mimeType: "image/png") 
      } 

      for (key,value) in parameter { 
       multipartFormData.append(value.data(using: .utf8)!, withName: key) 
      } 
     }, to: url,method: .post, headers: headers, encodingCompletion: { encodingResult in 
      switch encodingResult { 
      case .success(let upload,_,_): 
       upload.responseJSON { response in 

        print(response.request) 
        print(response.response) 
        print(response.result) 
        print(response.data) 
       } 
       break 
      case .failure(let encodingError): 
       print("error: \(encodingError)") 
       break 
      } 
     }) 

我的服務器接受BLOB數據中的圖像。如果有人能幫助我。謝謝!

回答

0

嘗試改變這一行:

multipartFormData.append(imageData, withName: "image", fileName: "file.png", mimeType: "image/png") 

要這樣:

multipartFormData.append(imageData, withName: "product_img", fileName: "image.jpg", mimeType: "image/jpeg") 
+0

相同的回答即失敗@chengsam –

+0

什麼是錯誤? – chengsam

+0

沒有錯誤,但當我嘗試打印結果時,它給出了一個失敗 –

0

請從下面的方法來得到答案(作品爲Alamofire 3.0+)取參考。

func uploadImageAndData(){ 

    var parameters = [String:AnyObject]() 
    parameters = ["token":token, 
        "lastName":lastName] 

    let URL = "http://yourserviceurl/" 
    let image = UIImage(named: "image.png") 

    Alamofire.upload(.POST, URL, multipartFormData: { 
     multipartFormData in 

     if let imageData = UIImageJPEGRepresentation(image, 0.6) { 
      multipartFormData.appendBodyPart(data: imageData, name: "image", fileName: "file.png", mimeType: "image/png") 
     } 

     for (key, value) in parameters { 
      multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key) 
     } 
    }, encodingCompletion: { 
     encodingResult in 

     switch encodingResult { 
     case .Success(let upload, _, _): 
      print("s") 
      upload.responseJSON { 
       response in 
       print(response.request) // original URL request 
       print(response.response) // URL response 
       print(response.data)  // server data 
       print(response.result) // result of response serialization 

       if let JSON = response.result.value { 
        print("JSON: \(JSON)") 
       } 
      } 
     case .Failure(let encodingError): 
      print(encodingError) 
     } 
    }) 
} 
+0

做了同樣的事情 –

+0

@shahinaliagharia沒有服務器端問題對不對? –

+0

服務器上沒有問題 –

相關問題