File upload 谷歌硬盘&x2B;Alamofire:上载具有属性的文件

File upload 谷歌硬盘&x2B;Alamofire:上载具有属性的文件,file-upload,swift2,google-drive-api,alamofire,File Upload,Swift2,Google Drive Api,Alamofire,我正试图通过Swift 2/Alamofire将一个文件+参数上传到Google Drive。在下面的代码中,我更改了以下行: "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart" 对下列事项: "https://www.googleapis.com/upload/drive/v3/files" 该文件上传到谷歌时没有名字。否则,文件上载将失败,并显示相同的通用代码: Error Domain=com.

我正试图通过Swift 2/Alamofire将一个文件+参数上传到Google Drive。在下面的代码中,我更改了以下行:

"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart"
对下列事项:

"https://www.googleapis.com/upload/drive/v3/files"
该文件上传到谷歌时没有名字。否则,文件上载将失败,并显示相同的通用代码:

Error Domain=com.alamofire.error Code=-6003 "Response status code was unacceptable: 400" UserInfo={NSLocalizedFailureReason=Response status code was unacceptable: 400}
我希望能够上传文件名和其他参数以及潜在的。我知道我不知怎么搞砸了多部分上传,但我不知道我做错了什么

  func postBinaryToGdriveSimple (token: String, callback: Bool -> Void){
var returnedId : String!
let path = NSBundle.mainBundle().pathForResource("drawing", ofType: "bin")

let bindata: NSData = NSData(contentsOfURL: NSURL(fileURLWithPath: path!))!
let parameters : [String: String] = ["title":"SomeFileName"]
let headers = ["Authorization": "Bearer \(token)"]
upload(
  .POST,
  "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
  headers: headers,

  multipartFormData: { multipartFormData in
    // append file parameters to request
    for (key, value) in parameters {
      multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
    }
    // append binary file to request
    multipartFormData.appendBodyPart(data: bindata, name: "upload", fileName: "drawing.bin", mimeType: "application/octet-stream")

  },
  encodingCompletion: { encodingResult in
    switch encodingResult {
    case .Success(let upload, _, _):
      upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
        dispatch_async(dispatch_get_main_queue()) {
          let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
          //progress(percent: percent)
          print ("................\(percent)")
        }
      }
      upload.validate()
      upload.responseJSON { response in
        switch response.result {
        case .Success(let data):
          print(response)
          print("Validation Successful")

          let json = JSON(data)
          returnedId = json[("id")].stringValue
          print("......id for uploaded file is \(returnedId)")

          callback(true)
        case .Failure(let error):
          print(error)
          print("Validation Bad")
          callback(false)
        }


      }
    case .Failure(_):
      callback(false)
    }
})
} // end of postBinaryToGdriveSimple
我想知道Alamofire创建多部分请求的方式是否有什么让Google Drive不喜欢的地方。从GoogleAPI站点上看,请求似乎需要有Alamofire可能没有创建的某些参数,例如内容长度和边界设置

POST /upload/drive/v3/files?uploadType=multipart HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer your_auth_token
Content-Type: multipart/related; boundary=foo_bar_baz
Content-Length: number_of_bytes_in_entire_request_body

--foo_bar_baz
Content-Type: application/json; charset=UTF-8

{
 "name": "My File"
}

--foo_bar_baz
Content-Type: image/jpeg

JPEG data
--foo_bar_baz--

如果是这样,解决方法是什么?

请仔细查看Google Drive的API文档

参数的键字段似乎是“name”(而不是“title”)

如果您需要限制为单个应用程序的其他自定义文件属性,请在json中添加“appProperties”:

“appProperties”:{ “头衔”:“无论什么”
}

非常感谢您关注我的问题。关于我对“name”参数的疏忽,您是对的……但是,这并没有解决问题。我更新了我的问题,加入了一个关于GoogleDrive API文档中多部分请求的简介……Alamofire是否可能没有发送请求所需的所有信息?如果是这样,如何修复?哦,好的,试着在multipartFormData闭包中注释for in循环,看看如果添加:multipartFormData.appendBodyPart(数据:“{'name':'FILE_name'}”).dataUsingEncoding(NSUTF8StringEncoding,allowLossyConversion:false)!name:“name”,mimeType:“application/json”)顺便说一句,边界由alamofire设置,当您“附加身体部位”时。AF方法应该是自提取文章的内容长度,并且mimeType参数设置请求的该部分的内容类型。明亮的再一次,你成功了。我喜欢这个答案的简单。multipartFormData.appendBodyPart(数据:“{'name':'FILE_name'}”).DataUsingEncode(N‌​SUTF8StringEncoding,allowLossyConversion:false)!,name:“name”,mimeType:“application/json”)解决了我的问题。非常感谢!酷。所以,只需添加一些颜色。。。在multipartFormData.appendBodyPart(数据:“data_TO_ENCODE”参数的主引号内添加的内容都已编码。请注意,这些主引号在Swift中定义了一个字符串,与参数字典变量一样,不包括在编码中。JSON需要{}、:和“”字符。为了便于回答,我使用单引号,但JSON标准是双引号,所以技术上应该使用\“(转义双引号,其中我有‘单引号字符’)。然后通过mimeType将内容类型设置为application/JSON。